How can I return NSJsonSerialization

一笑奈何 提交于 2019-12-04 05:49:11

问题


First, I know nothing about Objective-C.

That said the follow code should be getting the data from AsyncStorage.

I've already did something like for Android where the basic idea is get the data from AsyncStorage as Json Object.

What I need now, is use the jsonFromLocalRNStrogeForKey inside callInviteReceived

jsonFromLocalRNStrogeForKey source

+(void)jsonFromLocalRNStrogeForKey:(NSString *)key completion:(void (^)(NSDictionary * _Nullable, NSError * _Nullable))completion
{
    RCTResponseSenderBlock rnCompletion = ^(NSArray *response) {

        NSString *jsonAsString;

        if (response.count > 1) {
            NSArray *response1 = response[1];
            if (response1.count > 0) {
                NSArray *response2 = response1[0];                
                if (response2.count > 1) {
                    jsonAsString = response2[1];
                }
            }
        }

        NSData *jsonAsData = [jsonAsString dataUsingEncoding:NSUTF8StringEncoding];

        NSError *error;

        NSDictionary *json = [
            NSJSONSerialization
            JSONObjectWithData:jsonAsData
            options:NSJSONReadingMutableContainers
            error:&error
        ];

        completion(json, error);
    };

    RCTAsyncLocalStorage *storage = [RCTAsyncLocalStorage new];   

    dispatch_async(storage.methodQueue, ^{
        [storage performSelector:@selector(multiGet:callback:) withObject:@[key] withObject:rnCompletion];
    });
}

callInviteReceived

- (void)callInviteReceived:(TVOCallInvite *)callInvite {    
    NSJsonSerialization json = [self.jsonFromLocalRNStrogeForKey];
    // json.user.name
    [self reportIncomingCallFrom:@json.user.name withUUID:callInvite.uuid];
}

These methods should be working something like

-(NSJsonSerialization)jsonFromLocalRNStrogeForKey: {
    ...
    return json;
}

- (void)callInviteReceived {    
    ...

    NSJsonSerialization json = [self.jsonFromLocalRNStrogeForKey];

    // json.user.name
    [self reportIncomingCallFrom:json.user.name withUUID:callInvite.uuid];
}

So, is there anyone who could show me, how to code this?


Using RCTAsyncLocalStorage + getAllKeys


回答1:


The code would look something like this (You will need to supply the storageKey parameter):

- (void)callInviteReceived:(TVOCallInvite *)callInvite {    
    [self jsonFromLocalRNStrogeForKey:/*storageKey*/ completion:^(NSDictionary* data,NSError* error){
        if(data){
            NSString * name = [data valueForKeyPath@"user.name"];
            if(![name isKindOfClass:[NSNull class]]){
                [self reportIncomingCallFrom:name withUUID:callInvite.uuid];
            }
        }else{
            //handle error
            NSLog(@"JSON Parsing Error: %@",error.localizedDescription)
        }
   }
}

NSJSONSerialization is a utility class responsible for serializing and deserializing JSON. All of its methods are class methods, so it's pointless to pass it as a return value.

The output of deserialization is an NSDictionary which is a Map representation of the JSON. You can use this Map to extract values of user.name.



来源:https://stackoverflow.com/questions/48232786/how-can-i-return-nsjsonserialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!