Objective-C。 NSString 改为 NSCFNumber
Objective-C. NSString is changed to NSCFNumber
我在 Objective-C 中有一些代码。我想,我收到了 NSString 类型,但是当我尝试将它保存在 Core Data 中时,出现 user.clientID = clientID;
错误,例如:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for attribute: property = "clientID"; desired type = NSString; given type = __NSCFNumber; value = 29.'
所以,这是我的代码:
我的数据管理器单例PKDataManager.h
+(PKDataManager*) sharedManager;
- (void)updateCoreDataUserWithID:(NSString*) userID
andClientID:(NSString*) clientID;
PKDataManager.m
- (void)updateCoreDataUserWithID:(NSString*) userID
andClientID:(NSString*) clientID{
PKUser*user = [NSEntityDescription
insertNewObjectForEntityForName:@"PKUser"
inManagedObjectContext:self.managedObjectContext];
user.userID = userID;
user.clientID = clientID; ///// error showing here
[self.managedObjectContext insertObject:user];
[self.managedObjectContext save:nil];
}
我的单身人士 API PKServerManager.h
+ (PKServerManager*) sharedManager;
- (void) sendUserID:(NSString*) userID
onSuccess:(void(^)(NSString* clientID)) success
onFailure:(void(^)(NSError* error, NSInteger statusCode)) failure;
PKServerManager.m
- (void) sendUserID:(NSString*) userID
onSuccess:(void(^)(NSString* clientID)) success
onFailure:(void(^)(NSError* error, NSInteger statusCode)) failure{
NSDictionary* params =
[NSDictionary dictionaryWithObjectsAndKeys:
userID, @"userID", nil];
[self.requestOperationManager
POST:@"app/registration"
parameters:params
progress:nil
success:^(NSURLSessionTask *task, NSDictionary* responseObject) {
NSLog(@"JSON: %@", responseObject);
NSString* clientID = (NSString*)[responseObject objectForKey:@"clientId"];
if (success) {
success(clientID);
}
} failure:^(NSURLSessionTask *operation, NSError *error) {
}];
}
和我的代码:
PKProfile.m
[[PKServerManager sharedManager]
sendUserID:profile.userID
onSuccess:^(NSString* clientID) {
NSLog(@"clientID %@", clientID);
[[PKDataManager sharedManager] updateCoreDataUserWithID: profile.userID
andClientID: clientID];
}
onFailure:^(NSError *error, NSInteger statusCode) {
}];
在你的PKServerManager.m
替换以下代码行:
NSString* clientID = (NSString*)[responseObject objectForKey:@"clientId"];
和
NSString* clientID = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"clientId"]];
您的错误告诉您响应包含客户端 ID 的数值(并且在 responseObject
中表示为 NSNumber
;NSCFNumber
是更广泛的 NSNumber
class 簇)。您的 JSON 回复似乎包含类似以下内容:
{
...,
"clientId": 42
}
而不是
{
...,
"clientId": "42"
}
与其关注如何将此 NSNumber
转换为 NSString
,我更倾向于考虑将整个应用程序中的客户端 ID 更改为 NSInteger
(或NSNumber
)。如果这些 ID 确实是数值,最好有一个类型来反映这一点。
我在 Objective-C 中有一些代码。我想,我收到了 NSString 类型,但是当我尝试将它保存在 Core Data 中时,出现 user.clientID = clientID;
错误,例如:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for attribute: property = "clientID"; desired type = NSString; given type = __NSCFNumber; value = 29.'
所以,这是我的代码:
我的数据管理器单例PKDataManager.h
+(PKDataManager*) sharedManager;
- (void)updateCoreDataUserWithID:(NSString*) userID
andClientID:(NSString*) clientID;
PKDataManager.m
- (void)updateCoreDataUserWithID:(NSString*) userID
andClientID:(NSString*) clientID{
PKUser*user = [NSEntityDescription
insertNewObjectForEntityForName:@"PKUser"
inManagedObjectContext:self.managedObjectContext];
user.userID = userID;
user.clientID = clientID; ///// error showing here
[self.managedObjectContext insertObject:user];
[self.managedObjectContext save:nil];
}
我的单身人士 API PKServerManager.h
+ (PKServerManager*) sharedManager;
- (void) sendUserID:(NSString*) userID
onSuccess:(void(^)(NSString* clientID)) success
onFailure:(void(^)(NSError* error, NSInteger statusCode)) failure;
PKServerManager.m
- (void) sendUserID:(NSString*) userID
onSuccess:(void(^)(NSString* clientID)) success
onFailure:(void(^)(NSError* error, NSInteger statusCode)) failure{
NSDictionary* params =
[NSDictionary dictionaryWithObjectsAndKeys:
userID, @"userID", nil];
[self.requestOperationManager
POST:@"app/registration"
parameters:params
progress:nil
success:^(NSURLSessionTask *task, NSDictionary* responseObject) {
NSLog(@"JSON: %@", responseObject);
NSString* clientID = (NSString*)[responseObject objectForKey:@"clientId"];
if (success) {
success(clientID);
}
} failure:^(NSURLSessionTask *operation, NSError *error) {
}];
}
和我的代码: PKProfile.m
[[PKServerManager sharedManager]
sendUserID:profile.userID
onSuccess:^(NSString* clientID) {
NSLog(@"clientID %@", clientID);
[[PKDataManager sharedManager] updateCoreDataUserWithID: profile.userID
andClientID: clientID];
}
onFailure:^(NSError *error, NSInteger statusCode) {
}];
在你的PKServerManager.m
替换以下代码行:
NSString* clientID = (NSString*)[responseObject objectForKey:@"clientId"];
和
NSString* clientID = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"clientId"]];
您的错误告诉您响应包含客户端 ID 的数值(并且在 responseObject
中表示为 NSNumber
;NSCFNumber
是更广泛的 NSNumber
class 簇)。您的 JSON 回复似乎包含类似以下内容:
{
...,
"clientId": 42
}
而不是
{
...,
"clientId": "42"
}
与其关注如何将此 NSNumber
转换为 NSString
,我更倾向于考虑将整个应用程序中的客户端 ID 更改为 NSInteger
(或NSNumber
)。如果这些 ID 确实是数值,最好有一个类型来反映这一点。