【发布时间】:2017-03-08 23:27:50
【问题描述】:
将新字符串附加到旧字符串会导致崩溃。
(如果我这样做StructIOS.server = @""; StructIOS.server = [StructIOS.server stringByAppendingString:@".stackoverflow.com"];,则异常有效)。
struct.h:
struct iOS {
__unsafe_unretained NSString *input_url;
__unsafe_unretained NSString *use_url;
__unsafe_unretained NSString *server;
};
struct iOS StructIOS;
ViewController.m:
StructIOS.use_url = @"relay/pincode/apicode/accesskey/1/2/3/99/../900";
NSArray *work_array = [StructIOS.use_url componentsSeparatedByString:@"/"];
StructIOS.server = [work_array objectAtIndex:0];
if([StructIOS.server length] > 0) {
NSLog(@">>> 4: %@", StructIOS.server); // OK!!
StructIOS.server = [StructIOS.server stringByAppendingString:@".stackoverflow.com"]; // FAIL!!
NSLog(@">>> 5: %@", StructIOS.server);
}
输出:
>>> 4: relay
crash
预期输出:
>>> 5: relay.stackoverflow.com
编辑:以下方式工作没有崩溃
NSString *fool_ios;
// read from NSString type
StructIOS.server = fool_ios;
// save to NSString type
fool_ios = StructIOS.server;
【问题讨论】:
标签: ios objective-c nsarray