iPhone Xcode 数组列表可访问
目前正在开发我的第一个本机 iPhone 应用程序,该应用程序将集成到现有的 .net 应用程序中,并将使用 webServices。然而,我认为我面临的问题只不过是我对 xcode 的理解。
我希望我能解释这一点...
好吧,在我的第一个视图中,在 viewDidLoad 中,我转到网络服务并返回我的项目列表(这些填充表格视图),这完全可以正常工作:
--- snippet ---
- (void)viewDidLoad {
//GET ALL ITEMS
myArray = [[NSMutableArray alloc] init];
MyWebService *webService = [[MyWebService alloc] init];
myArray = [webService getAllNewsFunction];
[super viewDidLoad];
}
-- - 片段 ---
好吧,我现在已经填充了我的表格视图,并等待您选择您的项目(在本例中是新闻文章),我需要确定 selectedItem 以便使用文章的详细信息填充下一个视图。然而,在 didSelectRowAtIndexPath 方法中,我的数组列表似乎不再可访问。我现在非常困惑,因为如果我只是在当前视图中的 viewDidLoad 中创建一个项目列表而不使用我的网络服务,例如:
--- snippet ---
// listOfItems = [[NSMutableArray alloc] init];
// [myArray addObject:@"Iceland"];
// [listOfItems addObject:@"Greenland"];
// [listOfItems addObject:@"Switzerland"];
// [listOfItems addObject:@"Norway"];
// [listOfItems addObject:@"New Zealand"];
// [listOfItems addObject:@"Greece"];
// [listOfItems addObject:@"Italy"];
// [listOfItems addObject:@"Ireland"];
--- snippet ---
上面的内容可以在didSelectRowAtIndexPath,我可以填充详细视图。
您能帮我找出我面临的问题以及如何解决这个问题吗?我知道我已经向您提供了非常基础的知识,因此如果我需要提供更多信息,我很乐意这样做。
-- 更新 --
好的,根据您的回复,我更新了以下内容。
我现在已将 myArray 声明为属性
@property (nonatomic, retain) NSMutableArray *myArray;
,并将该行更新为:
self.myArray = [webService getAllNewsFunction];
-- update --
但是我现在在 self.myArray 行上收到错误“程序收到信号:“SIGABRT””。
有什么想法吗?
再次感谢
Currently developing my first Native iPhone application, the application is going to be integrating within an existing .net application and will be using webServices. However the problem i am facing i believe is nothing more than my understanding of xcode.
I hope i can explain this...
OK within my first view, within the viewDidLoad i go off to the webservice and return my list of items (these populate the tableview) this works completely fine:
--- snippet ---
- (void)viewDidLoad {
//GET ALL ITEMS
myArray = [[NSMutableArray alloc] init];
MyWebService *webService = [[MyWebService alloc] init];
myArray = [webService getAllNewsFunction];
[super viewDidLoad];
}
--- snippet ---
Ok so i now have my tableview populated and awaiting for you to select your item (in this case a news article) i need to determine the selectedItem in order to populate the next view with the details of the article. However it appears that within the didSelectRowAtIndexPath method my array list is no longer accessible. I am very confused at this point due to if i simply create a list of items within my viewDidLoad within the current view without using my webservice for example:
--- snippet ---
// listOfItems = [[NSMutableArray alloc] init];
// [myArray addObject:@"Iceland"];
// [listOfItems addObject:@"Greenland"];
// [listOfItems addObject:@"Switzerland"];
// [listOfItems addObject:@"Norway"];
// [listOfItems addObject:@"New Zealand"];
// [listOfItems addObject:@"Greece"];
// [listOfItems addObject:@"Italy"];
// [listOfItems addObject:@"Ireland"];
--- snippet ---
the above is accessible within didSelectRowAtIndexPath and i can populate the detailview.
Can you please help me pinpoint the problem i am facing and how this can be solved. I understand i have given you the very basics so if i need to provide more information i am happy to do so.
-- update --
ok based on your response i have update the following.
I have now declared myArray as a property
@property (nonatomic, retain) NSMutableArray *myArray;
and also updated the line to:
self.myArray = [webService getAllNewsFunction];
-- update --
however i now receive an error "program received signal: "SIGABRT"" on the self.myArray line.
any ideas?
Thanks Again
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您遵循命名约定,则方法
[webService getAllNewsFunction]
将返回一个自动释放的数组。因此,当您再次访问它时,它很可能已经被释放了。如果您使用
@property
声明myArray
(您应该避免这些问题),那么您可以通过执行以下操作来解决此问题:这行:
也是多余的,并且导致内存泄漏,因为您在分配/初始化的新
NSMutableArray
后立即将myArray
的值重新分配给[webService getAllNewsFunction]
d更新
因此,通过阅读您的更新,我认为您需要首先查看您的项目现在的警告,其中可能会显示如下内容:
问题的下一个线索出现在控制台中我得到类似的内容
因此,归根结底,您要么需要提供方法
,要么让编译器通过添加 @synthesize 来为您完成此操作像这样的语句
综合是更容易、更快的选项,编译器将根据您在 @property 声明中使用的选项来安排正确的内存管理。
If you are following naming conventions then the method
[webService getAllNewsFunction]
will return an autoreleased array. Therefore when you come to access it again it will most likely have been released already.If you have used a
@property
to declaremyArray
(which you should to save yourself from these problems) then you can resolve this by doing:This line:
is also superflous and causing a memory leak as you are reassigning the value of
myArray
to[webService getAllNewsFunction]
immediately after without releasing the newNSMutableArray
you alloc/init'dUPDATE
So from reading your update I think you need to firstly look at the warnings your project now has which will probably read something like:
The next clue to the problem appears in the console I get something like this
So what it all boils down to is you either need to provide the methods
or let the compiler do it for you by adding a
@synthesize
statement like thisThe synthesize is the easier, quicker options and the compiler will arrange for the coorect memory management depending on which options you use in your
@property
declaration.