开发者

Crashing Strings.... :(

开发者 https://www.devze.com 2023-01-01 02:25 出处:网络
I have in the .h file : NSString *dataHML; NSString *dataHML2; NSString *dataHML3; NSString *dataHML4; NSSt开发者_Go百科ring *dataHML5;

I have in the .h file :

NSString *dataHML; 
NSString *dataHML2;
NSString *dataHML3;
NSString *dataHML4;
NSSt开发者_Go百科ring *dataHML5;
NSString *dataHML6;
NSString *dataHMLtotal;

in the .m file I merge them with :

NSString *dataHtmlTotal = [NSString stringWithFormat:@"%@%@%@%@%@%@", dataHtml, dataHtml2, dataHtml3, dataHtml4,dataHtml5,dataHtml6];

But unfortunately it crashes at some point because of this. Could anyone give me a other solution and post it please, because I already tried nsuserdefault or a nsarray, but without I couldn't get it working.


If you really do have 6 variables numerically named like that, you could be better off with an array.

NSMutableArray *dataHMLStrings = [NSMutableArray array];

[dataHMLStrings addObject:@"String1"];
[dataHMLStrings addObject:@"String2"];
            .
            .
            .
[dataHMLStrings addObject:@"String100"]; // or however many you have.

NSString *dataHMLTotal = [dataHMLStrings componentsJoinedByString:@""];

You can give the componentsJoinedByString: method a different string (I passed an empty string here because you didn't want anything to appear between each dataHML string).


Please make sure your strings are all allocated and initialized (neither points of which you mention in your question.) If you do not do so then you run the risk of manipulating data at the location of the garbage pointers, and your application will very likely crash.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号