我在[NSString stringWithFormat:]
遇到了崩溃。我是从Crashlytics得到的。以下是我的应用程序最后一次调用之上的调用堆栈和其他信息
OS Version: 9.3.3 (13G34)
Device: iPhone 6s Plus
RAM Free: 8.7%
Disk Free: 20.9%
Crashed: com.apple.main-thread
CoreFoundation 0x1814224d0 __CFStringAppendFormatCore + 52
CoreFoundation 0x181422464 _CFStringCreateWithFormatAndArgumentsAux2 + 244
Foundation 0x181d3e320 -[NSPlaceholderString initWithFormat:locale:arguments:] + 168
Foundation 0x181d3e1f0 +[NSString stringWithFormat:] + 68
#import "ViewController.h"
#import <sys/sysctl.h>
static NSString *const kUserDefaultsPrefix = @"SDKData";
static NSString *const kVersionModel = @"VersionModel";
#define kOSVersion [[UIDevice currentDevice] systemVersion]
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self registerForMonitoring];
VersioningModel *currentVersion = [VersioningModel currentVersion];
}
- (void)registerForMonitoring {
[DataStorage addObserver:self forKeyPath:kVersionModel];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
VersioningModel *currentVersion = [VersioningModel currentVersion];
}
-(NSString *)osVersionBuild {
int mib[2] = {CTL_KERN, KERN_OSVERSION};
u_int namelen = sizeof(mib) / sizeof(mib[0]);
size_t bufferSize = 0;
NSString *osBuildVersion = nil;
// Get the size for the buffer
sysctl(mib, namelen, NULL, &bufferSize, NULL, 0);
u_char buildBuffer[bufferSize];
int result = sysctl(mib, namelen, buildBuffer, &bufferSize, NULL, 0);
if (result >= 0) {
osBuildVersion = [[NSString alloc] initWithBytes:buildBuffer length:bufferSize encoding:NSUTF8StringEncoding] ;
}
return osBuildVersion;
}
-(NSString *)formattedOSVersionAndBuild{
NSString *buildNumber = [self osVersionBuild] ?: nil;
if (!buildNumber) {
buildNumber = @"NONE";
}
buildNumber = [buildNumber stringByReplacingOccurrencesOfString:@"\0" withString:@""];
return [NSString stringWithFormat:@"%@-%@", kOSVersion, buildNumber];
}
@end
// Version Model class ====================
@implementation VersioningModel {
}
+ (VersioningModel *)currentVersion {
NSData *data = [DataStorage objectForKey:kVersionModel];
VersioningModel *versionModel = data?[NSKeyedUnarchiver unarchiveObjectWithData:data]:nil;
if (!versionModel){
NSLog(@"unarchiveObjectWithData failed");
versionModel = [[VersioningModel alloc] init];
[versionModel save];
NSLog(@"sucess");
}
return versionModel;
}
- (instancetype)init {
self = [super init];
if (self){
self.platformCode = @"iOS";
self.platformVersion = @"9.2-15F34";
self.sdkVersion = @"2.0.1";
}
return self;
}
-(void)save{
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self];
[DataStorage setObject:data forKey:kVersionModel];
}
//NSCoding
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
self = [super init];
if (self){
self.sdkVersion = [aDecoder decodeObjectForKey:@"sdkVersion"];
self.platformVersion = [aDecoder decodeObjectForKey:@"platformVersion"];
self.platformCode = [aDecoder decodeObjectForKey:@"platformCode"];
}
return self;
}
-(void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.sdkVersion forKey:@"sdkVersion"];
[aCoder encodeObject:self.platformVersion forKey:@"platformVersion"];
[aCoder encodeObject:self.platformCode forKey:@"platformCode"];
}
@end
// End Version Model Class ==================
// DataStorage class ====================
@implementation DataStorage {
}
+ (NSString *)prefixAndKey:(NSString *)key {
return [NSString stringWithFormat: @"%@%@", kUserDefaultsPrefix, key];
}
+ (id)objectForKey:(NSString *)key {
return [[NSUserDefaults standardUserDefaults] objectForKey:[self prefixAndKey:key]];
}
+ (void)setObject:(id)object forKey:(NSString *)key {
[[NSUserDefaults standardUserDefaults] setObject:object forKey:[self prefixAndKey:key]];
}
+ (void)addObserver:(id)object forKeyPath:(NSString *)key {
[[NSUserDefaults standardUserDefaults] addObserver:object forKeyPath:[self prefixAndKey:key] options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:nil];
}
@end
// End DataStorage class ====================