我是一位不了解目标c的敏捷开发人员。 谁能帮我转换一下Objective C中的以下代码
let newVersion = Int((userInfo.value(forKey:
"gcm.notification.version") as string).replacingOccurrences(of: ".", with: ""))
let currentVersion = Int((userDefaults.standard.value(forKey: "currentAppVersion") as string).replacingOccurrences(of: ".", with: ""))
if(newVersion > currentVersion) {
//code here
}
答案 0 :(得分:0)
NSString *newVersion = [(NSString *)[notification.userInfo valueForKey:@"gcm.notification.version"] stringByReplacingOccurrencesOfString:@"." withString:@""];
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
NSString *oldVersion = [(NSString *)[standardDefaults valueForKey:@"currentAppVersion"] stringByReplacingOccurrencesOfString:@"." withString:@""];
if ([newVersion integerValue] > [oldVersion integerValue]){
//code here
}
答案 1 :(得分:0)
应该执行以下操作:
NSDictionary *userInfo; // Assuming this is a dictionary
NSString *newVersionString = userInfo[@"gcm.notification.version"];
newVersionString = [newVersionString stringByReplacingOccurrencesOfString:@"." withString:@""];
NSInteger newVersion = [[[NSDecimalNumber alloc] initWithString:newVersionString] integerValue];
NSString *currentVersionString = [[NSUserDefaults standardUserDefaults] valueForKey:@"currentAppVersion"];
currentVersionString = [currentVersionString stringByReplacingOccurrencesOfString:@"." withString:@""];
NSInteger currentVersion = [[[NSDecimalNumber alloc] initWithString:currentVersionString] integerValue];
if(newVersion > currentVersion) {
//code here
}
但这不是您比较版本的方式。现在的问题是,例如1.11.1
等于11.1.1
。或者换句话说,无论如何您都可以重新定位点,但是如果数字的数量和它们的顺序保持不变,那么您会将其检测为相同的版本。您应该针对每个组件(在Swift或Objective-C中)执行此操作:
NSDictionary *userInfo;
NSArray<NSString *> *newComponents = [userInfo[@"gcm.notification.version"] componentsSeparatedByString:@"."];
NSString *currentVersionString = [[NSUserDefaults standardUserDefaults] valueForKey:@"currentAppVersion"];
NSArray<NSString *> *currentComponents = [currentVersionString componentsSeparatedByString:@"."];
BOOL newVersionIsGreater = NO;
if(newComponents.count != currentComponents.count) newVersionIsGreater = newComponents.count > currentComponents.count;
else {
for(int i=0; i<newComponents.count; i++) {
NSInteger newInteger = [[[NSDecimalNumber alloc] initWithString:newComponents[i]] integerValue];
NSInteger currentInteger = [[[NSDecimalNumber alloc] initWithString:currentComponents[i]] integerValue];
if(newInteger != currentInteger) {
newVersionIsGreater = newInteger > currentInteger;
break;
}
}
}
if(newVersionIsGreater) {
//code here
}
现在,这将检查两种情况下我们是否具有相同数量的组件,然后进行遍历。第一个不同的组件将报告更改。