【问题标题】:iPhone app quits randomly when accessing a NSStringiPhone 应用程序在访问 NSString 时随机退出
【发布时间】:2011-01-20 13:19:08
【问题描述】:

我的应用中有一个 NSString 问题。
我已经在视图控制器的头文件中定义了它。

    NSString *locationCoordinates;

我在 -(void) 方法中设置它的值。

- (void)locationUpdate:(CLLocation *)location {
    <...>

    NSArray *locArray = [locString componentsSeparatedByString:@", "];

    NSString *xCoordinate = [locArray objectAtIndex:0];
    NSString *yCoordinate = [locArray objectAtIndex:1]; 

    locationCoordinates = [NSString stringWithFormat:@"%@,%@", xCoordinate, yCoordinate];
}

在这种方法中,我可以将其打印到控制台

NSLog(locationCoordinates);

但如果我想在控制台中以其他方式查看它,我的应用会立即退出。

- (IBAction)saveAndReturnToRootView {
    NSLog(locationCoordinates);
}

控制台告诉我:

2010-02-24 14:45:05.399 MyApp[73365:207] *** -[NSCFSet length]: unrecognized selector sent to instance 0x4c36490
2010-02-24 14:45:05.400 MyApp[73365:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFSet length]: unrecognized selector sent to instance 0x4c36490'
2010-02-24 14:45:05.401 MyApp[73365:207] Stack: (
  32887899,
  2434934025,
  33269819,
  32839286,
  32691906,
  32417461,
  32527181,
  32527085,
  32747749,
  356942,
  630491,
  63461,
  2868313,
  4782069,
  2868313,
  3275682,
  3284419,
  3279631,
  2973235,
  2881564,
  2908341,
  40984273,
  32672640,
  32668744,
  40978317,
  40978514,
  2912259,
  9744,
  9598
)

我该如何解决这个问题?

提前致谢 ;-)

【问题讨论】:

    标签: iphone objective-c memory crash nsstring


    【解决方案1】:

    您没有保留字符串,因此正在清理内存。这会在您尝试访问它时导致崩溃。

    要保留它,您可以添加以下行

    [locationCoordinates retain];
    

    记得在你不再需要它时释放它——可能在你的类的析构函数中,否则你会出现内存泄漏。


    Objective C 中的标准做法是为此类成员使用属性。在头文件中使用

    @property (nonatomic, retain) NSString *locationCoordinates;
    

    然后在chuck中执行一个

    @synthesize locationCoordinates;
    

    当你访问 locationCoordinates 时,通过 self 访问它:

    self.locationCoordinates = [NSString stringWithFormat:@"%@,%@", xCoordinate, yCoordinate];
    

    Objective C 将创建一个 getter 和 setter 属性,以最有效的方式为您处理保留。

    顺便说一句,属性中的 nonatomic 关键字告诉objective c,您不需要它来围绕属性访问创建任何线程同步。如果你打算对这个类进行多线程处理,你应该考虑删除 nonatomic。这将确保属性访问是线程安全的。

    没有必要做任何你可以让编译器为你做的工作!

    【讨论】:

    • 太棒了,乐于助人!我终于设法充分了解了 Objective C 内存管理,以便能够回答有关它的问题。这是一个漫长的旅程..! ;-)
    【解决方案2】:

    在将字符串存储在类的变量中时,您应该保留该字符串:

    locationCoordinates = [NSString stringWithFormat:@"%@,%@", xCoordinate, yCoordinate];
    [locationCoordinates retain];
    

    原因是 [NSString stringWithFormat:...] 返回一个自动释放的实例。函数结束时字符串会自动释放。

    您也可以复制字符串:

    locationCoordinates = 
        [[NSString stringWithFormat:@"%@,%@", xCoordinate, yCoordinate] copy];
    

    当然,别忘了在dealloc中再次释放它:

    - (void) dealloc {
        [locationCoordinates release];
    
        [super dealloc];
    }
    

    【讨论】:

    • 如果你使用的是这个方法而不是属性,那么你需要在给 locationCoordinates 分配一个新值之前发送一个释放消息,否则如果 locationUpdate: 被调用超过一次。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-27
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多