NSMutableArray 无法正常工作...(计数不应该返回零!)
NSMutableArray 计数在添加对象后返回零,花了一个小时试图找出原因,但我仍然陷入困境,所以这把我带到了这里。
基于以下代码的任何想法,问题是什么?
对象“search”是在标头中定义为指针的自定义类,具有保留、非原子属性。
- (NSMutableArray *) populateArrayFromPlist{
NSLog(@"Populate Array from PList");
NSDictionary *dictionary;
// read "foo.plist" from application bundle
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"asearch.plist"];
dictionary = [NSDictionary dictionaryWithContentsOfFile:finalPath];
for (id key in dictionary)
{
search = [[ASearch alloc] init];
[dictionary valueForKey:key];
[search setID:[[dictionary valueForKey:key] intValue] ];
//[[search searchString] initWithString: key];
search.searchString = [[NSMutableString alloc] initWithString: key];
if (search == nil) {
printf("Let me know now\n\n\n\n");
}
NSLog(@"%@", [search searchString]);
NSLog(@"Setting string Value: %s\n", [key cString]);
NSLog(@"Setting ID Value: %i\n", [[dictionary valueForKey:key] intValue]);
//NSLog(@"aSearchArray count == %i", [[aSearchArray count] intValue]);
[aSearchArray addObject:search];
NSLog(@"aSearchArray count == %i", [aSearchArray count] );
NSMutableArray count is returning zero after adding objects to it, its been an hour of hacking away trying to figure out why, and I'm still stuck, so that brings me here.
Any ideas based off the following code, what the problem is?
the object 'search' is a custom class defined in the header set as a pointer, with retain, nonatomic attributes.
- (NSMutableArray *) populateArrayFromPlist{
NSLog(@"Populate Array from PList");
NSDictionary *dictionary;
// read "foo.plist" from application bundle
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"asearch.plist"];
dictionary = [NSDictionary dictionaryWithContentsOfFile:finalPath];
for (id key in dictionary)
{
search = [[ASearch alloc] init];
[dictionary valueForKey:key];
[search setID:[[dictionary valueForKey:key] intValue] ];
//[[search searchString] initWithString: key];
search.searchString = [[NSMutableString alloc] initWithString: key];
if (search == nil) {
printf("Let me know now\n\n\n\n");
}
NSLog(@"%@", [search searchString]);
NSLog(@"Setting string Value: %s\n", [key cString]);
NSLog(@"Setting ID Value: %i\n", [[dictionary valueForKey:key] intValue]);
//NSLog(@"aSearchArray count == %i", [[aSearchArray count] intValue]);
[aSearchArray addObject:search];
NSLog(@"aSearchArray count == %i", [aSearchArray count] );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
aSearchArray 是一个 nil 对象,这是您计数为零的唯一原因。
如需更多确认,只需创建一个新的本地数组并尝试将您的对象添加到其中。
您将得到正确的计数
aSearchArray is a nil object that is the only reason why you are getting count as zero.
For more confirmation just create a new local array and try to add your object to it.
You will get a proper count