iOS 5 中的 NSMutable 数组
我正在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这与 iOS 5 无关,而是 Objective C 的编译器错误。基本问题是您同时使用
MySpots
作为接口名称和属性/实例变量。当遇到您的行时,编译器现在不确定这是否是变量
MySpots
还是应该是MySpots
类型变量的声明,例如编译器假定第二个并退出因为缺少变量名。
您应该将该
其中任何一个都会有帮助(在情况 1 中,编译器现在清楚要做什么,在情况 2
self.MySpots
将是一个唯一的名称,不能与类名混淆),但是我肯定会同时执行这两个操作,重命名属性(或类)并使用 self.property = 。PS:即使使用 ARC,您仍然可以像往常一样使用
[[NSMutableArray alloc] init]
进行初始化,ARC 只是在后面插入适当的retain
和releases
场景。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 linethe compiler now is unsure if this is the variable
MySpots
or should be a declaration of a variable of typeMySpots
, e.g.The compiler assumes the second and bails out because of the missing variable name.
You should
self.property =
instead ofproperty =
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 useself.property =
.PS: Even with ARC you still initialize as usual by using
[[NSMutableArray alloc] init]
ARC just inserts the appropriateretain
andreleases
behind the scenes.确切的代码永远无法编译,更不用说正确运行了!
首先:您在循环中使用的局部变量
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.