我访问NSMutableArray(nid)的特定元素并将其保存在NSString(num)中。
之后我想要将这个元素(num)与url(NSMutableString)连接起来,并使用appendString方法,但我获得了一个异常。
我该如何解决这个问题?
代码下面的:
NSMutableArray nid;
....
NSUInteger row = [indexPath row];
NSString *num = [nid objectAtIndex:row];
NSMutableString *url = [[NSMutableString alloc]
initWithString:@"http://example.com/prova/"];
[url appendString:num];
答案 0 :(得分:4)
- [NSCFNumber length]:发送到实例0x4b54de0的无法识别的选择器 * 因未捕获的异常而终止应用 'NSInvalidArgumentException',原因: ' - [NSCFNumber length]:无法识别 选择器发送到实例0x4b54de0'
根据例外情况,objectAtIndex:
似乎正在返回NSNumber
而不是NSString
。如果nid
包含数字,则需要先将它们转换为字符串,然后才能将其传递给appendString
:
NSUInteger row = [indexPath row];
NSNumber *num = [nid objectAtIndex:row];
NSString *numStr = [num stringValue];
NSMutableString *url = [[NSMutableString alloc]
initWithString:@"http://example.com/prova/"];
[url appendString:numStr];
...
[url release];
答案 1 :(得分:1)
nid 是array
,它返回的数字不是NSString,因此以下语句返回NSNumber
NSString *num = [nid objectAtIndex:row]; // Wrong statement
NSNumber* num = [nid objectAtIndex:row]; // Correct statement
所以你的代码应该是..
NSMutableArray nid;
....
NSUInteger row = [indexPath row];
//your array contain NSNumber type of pointer NOT NSString.
NSNumber *num = [nid objectAtIndex:row];
//Get a NSString object from a NSNumber.
NSString *myString = [num stringValue];
NSMutableString *url = [[NSMutableString alloc]
initWithString:@"http://example.com/prova/"];
[url appendString:myString];