作者Gsus (肉元,吉米,助教)
看板MacDev
标题[问题] iphone上将档案写入的问题
时间Mon Aug 16 23:52:08 2010
我有一段code如下
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
if (paths.count > 0) {
NSString *loginInfoFilePath =
[[paths objectAtIndex:0] stringByAppendingFormat:@"loginInfo.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
// if loginInfo.plist do not exists:
if (![fileManager fileExistsAtPath:loginInfoFilePath]) {
NSDictionary *loginInfoDictionary = [[NSDictionary alloc]
initWithObjectsAndKeys:@"", nicknameKey, @"", passwordKey, nil];
[loginInfoDictionary writeToFile:loginInfoFilePath atomically:YES];
[loginInfoDictionary release];
}
// here loginInfo.plist should always exists
if ([fileManager fileExistsAtPath:loginInfoFilePath]) {
// do something here.
}
上面总共有3个if
我在simulator上测试,app第一次launch时3个if都有跑到
可是换在device上测试的时候,一样是第一次launch
却唯独第3个if跑不进去
问题可能出在writeToFile没有成功
可是我还不确定
而且另我纳闷的是为什麽simulator可以,device却会有不一样的结果QQ
是哪边出问题了吗~
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 111.251.146.190
1F:推 wasihero:可能是延迟写入的关系吧..第二次执行的情况咧? 08/17 01:15
2F:→ Gsus:第二次的状况也是一样QQ 08/17 07:26
3F:→ Gsus:找到问题了,在loginInfoFilePath那边的append string应该是 08/17 09:26
4F:→ Gsus:@"/loginInfo.plist" 08/17 09:26
5F:→ zonble:如果是处理路径的话,用stringByAppendingPathComponent: 08/17 12:17
6F:→ Gsus:可是我就是用stringByAppend...啊 QQ 08/17 22:12
7F:→ zonble:不一样耶 08/17 22:50
8F:→ zonble:append string 有几个不同的 method 08/17 22:50
9F:→ zonble:直接 append 一个 string,用 stringByAppendingString: 08/17 22:51
10F:→ zonble:目录後面接档名,是stringByAppendingPathComponent: 08/17 22:52
11F:→ zonble:档名後面加附档名,是stringByAppendingPathExtension: 08/17 22:52
12F:→ zonble:而你用的是 stringByAppendingFormat: 而你这样写很危险 08/17 22:53
13F:→ zonble:因为这个 method 是让你用一个格式把资料填进去的 08/17 22:53
14F:→ zonble:例如 stringBy.,,:@"%@ %d", stringValue, intValue 08/17 22:54
15F:→ zonble:但是你後面又没有加上要供 format 字串用的参数 08/17 22:54
16F:→ zonble:这样假如你的字串里头有 %@, %d 这些东西,都会出问题 08/17 22:55
17F:→ Gsus:喔喔喔!受教了! 感谢~~~ 08/18 00:41
18F:推 antirazin:推荐这一篇 10/17 15:42