iOS 5 中的 NSMutable 数组

发布于 2024-12-18 19:56:58 字数 909 浏览 3 评论 0原文

我正在 iOs 5 中使用故事板创建一个应用程序,在该应用程序中我需要使用一个 NSMutableArray 作为表视图。 所以我正在创建该数组,如下所示。 创建类似 和 的数组

@interface MySpots : UITableViewController{
       NSMutableArray *MySpots;
}
@property (nonatomic, strong) NSMutableArray *MySpots;

但在 iOs 4 中,我们在 .m 文件中

@synthesize MySpots;
- (void)viewDidLoad

{
     [super viewDidLoad];
      MySpots = [[NSMutableArray alloc]init];
      for (int i = 0; i<10; i++) {
          mySpot.SpotName = @"Name Of Spot";
          mySpot.SpotTime = @"15mi";
          mySpot.SpotDis = @"Short preview of the description will appear in this section.Acts as a reminder for relevant";
          [MySpots addObject:mySpot];
       }

}

,但是当我尝试在 iOs 5 中执行此操作时,它会显示错误(“预期标识符或 {” ),

MySpots = [[NSMutableArray alloc]init];

因为在 iOs 5 中不需要释放。那么我应该如何在 iOs 5 中声明 NSMutableArray 。 提前致谢 :)

I am creating one application in iOs 5 using story board and in that application I need to use one NSMutableArray for table View.
so I am creating that array as shown below.
but in iOs 4 we creating array like

@interface MySpots : UITableViewController{
       NSMutableArray *MySpots;
}
@property (nonatomic, strong) NSMutableArray *MySpots;

and in .m file

@synthesize MySpots;
- (void)viewDidLoad

{
     [super viewDidLoad];
      MySpots = [[NSMutableArray alloc]init];
      for (int i = 0; i<10; i++) {
          mySpot.SpotName = @"Name Of Spot";
          mySpot.SpotTime = @"15mi";
          mySpot.SpotDis = @"Short preview of the description will appear in this section.Acts as a reminder for relevant";
          [MySpots addObject:mySpot];
       }

}

but when i try to do this in iOs 5 it will show me error("Expected identifier or {" ) on

MySpots = [[NSMutableArray alloc]init];

because in iOs 5 no need to release. so how should i have to declare NSMutableArray in iOs 5 .
thanks in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

本宫微胖 2024-12-25 19:56:58

这与 iOS 5 无关,而是 Objective C 的编译器错误。基本问题是您同时使用 MySpots 作为接口名称和属性/实例变量。当遇到您的行时,

MySpots = [[NSMutableArray alloc]init];

编译器现在不确定这是否是变量 MySpots 还是应该是 MySpots 类型变量的声明,例如

MySpots *mySecondController = ...;

编译器假定第二个并退出因为缺少变量名。

您应该将该

  1. 属性的名称更改为与类名称不同的名称,以避免混淆,
  2. 使用 self.property = 而不是 property = ,因为这使用了正确的内存管理。 ARC 在一定程度上减少了这个问题,但无论如何,使用属性提供的自动保留/释放功能都是很好的做法。

其中任何一个都会有帮助(在情况 1 中,编译器现在清楚要做什么,在情况 2 self.MySpots 将是一个唯一的名称,不能与类名混淆),但是我肯定会同时执行这两个操作,重命名属性(或类)并使用 self.property = 。

PS:即使使用 ARC,您仍然可以像往常一样使用 [[NSMutableArray alloc] init] 进行初始化,ARC 只是在后面插入适当的 retainreleases场景。

This has nothing to do with iOS 5 but rather is a compiler error from objective C. The basic problem is that you use MySpots both as interface name and as property/instance variable. When encountering your line

MySpots = [[NSMutableArray alloc]init];

the compiler now is unsure if this is the variable MySpots or should be a declaration of a variable of type MySpots, e.g.

MySpots *mySecondController = ...;

The compiler assumes the second and bails out because of the missing variable name.

You should

  1. change that property's name to something different than the class name to avoid confusion
  2. use self.property = instead of property = because this uses the correct memory management. ARC somewhat reduces this problem but it is good practice anyway to use the automatic retain/release provided by properties.

Either one of those would help (in case 1 the compiler now is clear on what to do, in case 2 self.MySpots would be a unique name and could not be mixed up with the class name) but I would definitely do both, rename the property (or class) and use self.property =.

PS: Even with ARC you still initialize as usual by using [[NSMutableArray alloc] init] ARC just inserts the appropriate retain and releases behind the scenes.

笑脸一如从前 2024-12-25 19:56:58

确切的代码永远无法编译,更不用说正确运行了!

首先:您在循环中使用的局部变量 mySpot 首先来自哪里?

其次,我非常确定您不能将变量命名为相同类型:MySpots既是一个类,又是该类型的ivar同一个班?
如果该声明适用于某些奇怪的编译器,我会认为这是编译器中的错误。即使 if 该声明已编译 - 这已经是非常多的 if - 您使用该变量的行无法工作,因为编译器无法知道您是否指的是该类型MySpots 或实例变量 - 到目前为止编译的奇怪的东西可能在这里做一些事情,但我不会相信这样一个“编译器”的代码输出的有效性”。

这就是您的错误消息的来源。

That exact code can never ever have compiled, let alone worked correctly!

First of all: where does the local variable mySpot you are using in your loop come from in the first place?

Second, I am pretty much positively sure that you cannot name a variable identical to a type: MySpots is both a class and an ivar of that same class?
If that declaration worked on some weird compiler, I would consider that a bug in the compiler. And even if that declaration compiled — that's an awful lot of ifs, already — the line where you use that variable cannot work, because the compiler cannot know whether you meant the type MySpots or the instance variable — the weird thing that compiled thus far may do something here but I would not put any trust in the validity of the code output of such a "compiler".

And that's where your error message comes from.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文