Appending new string to old string is causing crash.
(abnormally works if i do as like this 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);
}
Output:
>>> 4: relay
crash
Expected output:
>>> 5: relay.stackoverflow.com
EDIT: following way worked without crash
NSString *fool_ios;
// read from NSString type
StructIOS.server = fool_ios;
// save to NSString type
fool_ios = StructIOS.server;
Why are they __unsafe_unretailed? componentsSeparatedByString() will presumably internally be creating some substrings which you are not taking ownership of due to using __unsafe_unretained, thus they are being deleted when componentsSeparatedByString function got out of scope. The log at line 4 is working purely though chance and good luck as the string is deallocated but still there at that memory location. If you rewrite the struct as a simple class (which inherits from NSObject) it should work.