iphone - EXC_BAD_ACCESS 带有 characterAtIndex,但不是 substringToIndex

标签 iphone objective-c ios

我是 Objective-C 的新手,作为一个非常简单的学习练习,我创建了一个方法来返回存储名称的变量的首字母:

-(NSString *)initial
{
    return [_name substringToIndex:1];
}

当我使用 substringToIndex 时,这完全符合预期,但是如果我改为使用 characterAtIndex,我会收到 EXC_BAD_ACCESS 错误。

谁能解释为什么一个有效而另一个无效?

最佳答案

characterAtIndex 返回一个 char,而不是 (NSString *),问题可能在于返回错误的类型。

关于iphone - EXC_BAD_ACCESS 带有 characterAtIndex,但不是 substringToIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7854669/

相关文章:

iphone - setFinishedSelectedImage :withFinishedUnselectedImage: + Storyboards

iphone - 如何创建像 alertview 一样填充或调暗背景的 UIView?

ios - 如果太高,使用自动布局调整距水平中心的垂直位置

ios - 使用 UISegmentedControl 撤消选择

ios - 如何在 Firebase 中删除具有多个子对象的对象?

objective-c - didActivateNotification 没有响应 NSUserNotification

ios - 像 Facebook 这样的 iOS 侧边栏菜单实现?

iphone - 我如何检测 rss 提要中的新项目

iphone - UIView 内的 UIViewController 没有获得轮换委托(delegate)

iphone - 在 iPhone 和 iPad 上使用 Quartz 绘制阴影速度很慢。其他方式?