为什么我的NSMutableArray initWithContentsOfFile返回Null?

时间:2014-09-15 16:22:20

标签: objective-c nsmutablearray

我遇到了NSMutableArray的问题。

当我记录此代码时:

cityObject = [citiesArray objectAtIndex:indexPath.row];
NSLog(@"%@", cityObject.cityName);

它记录:

2014-09-15 11:12:55.599 JSONStoryboard[1865:707465] London
2014-09-15 11:12:55.605 JSONStoryboard[1865:707465] Bombay
2014-09-15 11:12:55.609 JSONStoryboard[1865:707465] Kuala Lumpur
2014-09-15 11:12:55.612 JSONStoryboard[1865:707465] New York
2014-09-15 11:12:55.615 JSONStoryboard[1865:707465] Berlin
2014-09-15 11:12:55.618 JSONStoryboard[1865:707465] Duesseldorf

但是当我尝试在这里使用initWithContentsOfFile时,它并没有记录我的预期:

self.namesArray = [[NSMutableArray alloc]initWithContentsOfFile:cityObject.cityName];
NSLog(@"%@", self.namesArray);

结果是:

2014-09-15 11:16:26.241 JSONStoryboard[1875:708269] (null)
2014-09-15 11:16:26.247 JSONStoryboard[1875:708269] (null)
2014-09-15 11:16:26.250 JSONStoryboard[1875:708269] (null)
2014-09-15 11:16:26.253 JSONStoryboard[1875:708269] (null)
2014-09-15 11:16:26.256 JSONStoryboard[1875:708269] (null)
2014-09-15 11:16:26.259 JSONStoryboard[1875:708269] (null)

当我希望它记录我的城市名称时。

这是什么问题?如何将我的城市名称添加到namesArray?

1 个答案:

答案 0 :(得分:0)

你不太可能在路径上找到文件" London"," Bombay"因为这些甚至不是有效的路径。因此initWithContentsOfFile将返回nil。

找出文件的实际存储位置,并创建每个文件的路径。例如

NSString* myDataDirectory = [self whereIStoreAllTheFiles];
NSString* cityPath = [myDataDirectory stringByAddingPathComponent:@"London"];
NSArray* cityData = [[NSArray alloc] initWithContentsOfFile:cityPath]; 

代码未经过任何方式测试。