![](/img/trans.png)
[英]Terminating app due to uncaught exception 'NSInvalidArgumentException' unrecognized selector sent to instance Objective-C Singleton
[英]Terminating the app due to uncaught exception in Objective-C for iPhone app
我已经在Objective-C中编写了一个for循环,这就是我的代码的样子
NSString *string = [NSString stringWithContentsOfFile=@"/Users/Home/myFile.doc"];
NSString *seperator=@"\n";
NSArray *mainarray = [string componentsSeparatedByString:seperator];
// Since i want to parse each element of mainarray
for(NSString *s in mainarray)
{
//again parising the string using a new separator
NSString newseparator = @"=";
NSArray *subarray = [s componentsSeparatedByString : newseparator];
//Copying the elements of array into key and object string variables
NSString *key = [subarray objectAtIndex:0];
NSLog(@"%@",key);
NSString *class_name= [subarray objectAtIndex:1];
NSLog(@"%@",class_name);
// create an instance for the class_name
//dont knw how it ll take the value from file and ???
//Putting the key and objects values into hashtable
NSMutableDictionary = [NSDictionary dictinaryWithObject:class_name forKey:key];
}
每当我执行此代码时,这会使我的程序崩溃,因为由于未捕获的异常NSRangeException而终止了应用程序
如何知道数组的范围以及如何在for循环中指定终止条件?而plz让我知道如何处理此异常?
我很惊讶代码甚至可以编译。 如果我没记错的话,除非您竭尽全力关闭一堆编译器警告,否则它就无法编译。
NSString newseparator = @";";
那应该给你写一个错误,因为你没有*。
NSString *key = [subarray objectAtIndex[0]];
NSString *object = [subarray objectAtIndex[1]];
这些代码行都没有任何意义。
看来您尚未发布实际代码?
现在,回到异常。 如果您尝试以数组中可用索引范围之外的索引访问项目,则会抛出范围异常。 因此,如果componentsSeparatedByString:
返回一个包含0或1个元素的数组,则[subarray objectAtIndex: 1];
会引起范围异常。
什么你不想做的是尽量使用处理异常@catch
块。 在Cocoa(和iPhone开发)中,异常被视为不可恢复的错误。
因此,相反,请在NSArray上使用-count
方法来验证数组是否实际包含了您期望的元素数。 由于您正在编写一个临时分析器,所以这可能是一个对输入有效性进行最小检查的好主意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.