我在initWithFormat:arguments命令上遇到突然崩溃;我怀疑它取决于一些内存问题,因为崩溃是非常不可靠的;我几个月就有这个代码,就在今天它开始崩溃了!在我的代码中是否有一些明显的问题导致悬挂引用,我对Objective-c中的C结构并不十分熟悉。
- (id)stringWithFormat:(NSString *)format array:(NSArray*) arguments{
if ([arguments count]==0) return format;
NSLog(@"argument counts %lu", (unsigned long)[arguments count]);
NSRange range = NSMakeRange(0, [arguments count]);
NSMutableData* data = [NSMutableData dataWithLength:sizeof(id) * [arguments count]];
[arguments getObjects:(__unsafe_unretained id *)data.mutableBytes range:range];
NSLog(@"format %@ arguments=%@", format, arguments);
NSString* result = [[NSString alloc] initWithFormat:format arguments:data.mutableBytes];
return result;
}
格式和参数匹配,正如我在NSLog中看到的那样。 这是崩溃日志:
线程0崩溃: 0 libobjc.A.dylib 0x0000000102152fc5 0x10213a000 + 102341 1基金会0x0000000101ad9137 0x101a99000 + 262455 2 CoreFoundation
0x0000000105278244 0x105273000 + 21060 3 CoreFoundation
0x00000001052b6913 0x105273000 + 276755 4基金会 0x0000000101ad80b2 0x101a99000 + 258226 5 inArrivoHD
0x00000001000041ea 0x100000000 + 16874 6 inArrivoHD
0x0000000100004445 0x100000000 + 17477 7 inArrivoHD
0x00000001000069f0 0x100000000 + 27120 8 inArrivoHD
0x0000000100006ef0 0x100000000 + 28400 9 libdispatch.dylib
0x000000010570c851 0x10570b000 + 6225 10 libdispatch.dylib
0x000000010571f72d 0x10570b000 + 83757 11 libdispatch.dylib
0x000000010570f3fc 0x10570b000 + 17404 12 CoreFoundation
0x0000000105351289 0x105273000 + 909961 13 CoreFoundation
0x000000010529e854 0x105273000 + 178260 14 CoreFoundation
0x000000010529dd83 0x105273000 + 175491 15 GraphicsServices
0x000000010236af04 0x102360000 + 44804 16 UIKit
0x0000000100ab3e33 0x100a9f000 + 85555 17 inArrivoHD
0x00000001000013a3 0x100000000 + 5027 18 libdyld.dylib
0x00000001059705fd 0x10596d000 + 13821
答案 0 :(得分:0)
最后,我带来了这个至少有效的愚蠢功能:
+ (NSString *)stringWithFormat:(NSString *)format arguments:(NSArray *) arguments
{
return [NSString stringWithFormat:format,
(arguments.count > 0) ? arguments[0]: nil,
(arguments.count > 1) ? arguments[1]: nil,
(arguments.count > 2) ? arguments[2]: nil,
...
(arguments.count > 20) ? arguments[20]: nil];
}