NSArray 组件SeparatedByString Sigabrt
我在 NSlog 上收到了 Sigabrt,但我不知道为什么 - 有什么建议吗?
NSString* contentList = [NSString stringWithContentsOfFile:currentFilePath encoding:NSUTF8StringEncoding error:nil];
NSArray* contentArray = [contentList componentsSeparatedByString:@"$$"];
NSLog(@"%@%@",contentList,[contentArray count]);
kunden = [contentArray objectAtIndex:0];
kundenView.text = kunden;
按照乔的建议,我现在得到了:
NSString* contentList = [NSString stringWithContentsOfFile:currentFilePath encoding:NSUTF8StringEncoding error:nil];
NSArray* contentArray = [[contentList componentsSeparatedByString:@"$$"] retain];
if ([contentArray count] > 0) {
NSLog(@"%@%@",contentList,[contentArray count]);
kunden = [contentArray objectAtIndex:0];
kundenView.text = kunden;
}
这给了我一个关于 NSLog 的 EXC_BAD_ACCESS 。
I get a Sigabrt at the NSlog and i have no idea why - any suggestions?
NSString* contentList = [NSString stringWithContentsOfFile:currentFilePath encoding:NSUTF8StringEncoding error:nil];
NSArray* contentArray = [contentList componentsSeparatedByString:@"$"];
NSLog(@"%@%@",contentList,[contentArray count]);
kunden = [contentArray objectAtIndex:0];
kundenView.text = kunden;
Following Joes suggestions, I now got:
NSString* contentList = [NSString stringWithContentsOfFile:currentFilePath encoding:NSUTF8StringEncoding error:nil];
NSArray* contentArray = [[contentList componentsSeparatedByString:@"$"] retain];
if ([contentArray count] > 0) {
NSLog(@"%@%@",contentList,[contentArray count]);
kunden = [contentArray objectAtIndex:0];
kundenView.text = kunden;
}
Which gives me an EXC_BAD_ACCESS at the NSLog thing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 NSLog 语句正在尝试打印一个整数,就像它是一个对象一样:
将
%@
替换为%d
。您可以在 String 中阅读有关格式说明符的更多信息编程指南。
Your NSLog statement is trying to print an integer as if it was an object:
Replace
%@
with%d
.You can read more on format specifiers in the String Programming Guide.
您没有检查以确保数组中至少有 1 个元素。如果
contentArray
为空,则访问[contentArray objectAtIndex:0]
将出现问题。You are not checking to make sure you have at least 1 element in your array. Accessing
[contentArray objectAtIndex:0]
will be an issue if thecontentArray
is empty.