2 hyg0406 hyg0406 于 2014.12.02 16:05 提问

ios开发中,运用nsfilehander写文件时报错,本人菜鸟,请教大神

NSString *path = NSHomeDirectory();

NSString *filePath = [path stringByAppendingString:@"/test.txt"];

BOOL success =[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];

if (success) {
NSLog(@"创建成功");
}

NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];

NSArray *array = [NSArray arrayWithObjects:@"a",@"b",@"c", nil];

for (int i=0;i<3; i++) {
data = [array objectAtIndex:i] ;

[fileHandle writeData:data  ];

}

[fileHandle closeFile];

报错代码:
 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString enumerateByteRangesUsingBlock:]: unrecognized selector sent to instance 0x6c090'

*** First throw call stack:
(
0 CoreFoundation 0x008bedf6 exceptionPreprocess + 182
1 libobjc.A.dylib 0x00548a97 objc_exception_throw + 44
2 CoreFoundation 0x008c6a75 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3 CoreFoundation 0x0080f9c7 ___forwarding
_ + 1047
4 CoreFoundation 0x0080f58e CF_forwarding_prep_0 + 14
5 Foundation 0x001597c9 -[NSConcreteFileHandle writeData:] + 142
6 联系人管理 0x00069877 -[ViewController viewDidLoad] + 487
7 UIKit 0x00dc6d54 -[UIViewController loadViewIfRequired] + 771
8 UIKit 0x00dc7045 -[UIViewController view] + 35
9 UIKit 0x00cbed65 -[UIWindow addRootViewControllerViewIfPossible] + 66
10 UIKit 0x00cbf1dc -[UIWindow _setHidden:forced:] + 319
11 UIKit 0x00cbf473 -[UIWindow _orderFrontWithoutMakingKey] + 49
12 UIKit 0x00ccd615 -[UIWindow makeKeyAndVisible] + 80
13 UIKit 0x00c6acd6 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 3108
14 UIKit 0x00c6e16d -[UIApplication _runWithMainScene:transitionContext:completion:] + 1639
15 UIKit 0x00c86d30 __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke + 59
16 UIKit 0x00c6cd7f -[UIApplication workspaceDidEndTransaction:] + 155
17 FrontBoardServices 0x031f59de __37-[FBSWorkspace clientEndTransaction:]_block_invoke_2 + 71
18 FrontBoardServices 0x031f546f __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 54
19 FrontBoardServices 0x03207425 __31-[FBSSerialQueue performAsync:]_block_invoke + 26
20 CoreFoundation 0x007e27a0 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK
_ + 16
21 CoreFoundation 0x007d80b3 __CFRunLoopDoBlocks + 195
22 CoreFoundation 0x007d7f0b __CFRunLoopRun + 2715
23 CoreFoundation 0x007d71ab CFRunLoopRunSpecific + 443
24 CoreFoundation 0x007d6fdb CFRunLoopRunInMode + 123
25 UIKit 0x00c6c744 -[UIApplication _run] + 571
26 UIKit 0x00c6fe16 UIApplicationMain + 1526
27 联系人管理 0x0006aa8d main + 141
28 libdyld.dylib 0x02c0cac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

1个回答

single010203
single010203   2014.12.02 17:20

顶起来,我也想知道这个问题

hyg0406
hyg0406 没有设置编码的原因,data = [[array objectAtIndex:i] dataUsingEncoding:NSUTF8StringEncoding];
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!