Daniel
Daniel

Reputation: 285

NSArray key => value,

My key value current system isn't working. Here where the array is set.

NSArray *matchInfo = [[NSArray alloc] initWithObjects:
                          @{@"matchName" : _matchName},
                          @{@"hostName" : _hostName},
                          nil];

Then is it converted into NSData

NSData *dataToSend = [NSKeyedArchiver archivedDataWithRootObject:message];

Received and set

    NSArray *receivedArray = [NSKeyedUnarchiver unarchiveObjectWithData:receivedData];

    _matchName = [receivedArray valueForKey:@"matchName"];
    _hostName = [NSString stringWithFormat:@"%@",[receivedArray valueForKey:@"hostName"]];

    NSLog(@"%@, %@", _matchName, _hostName);

The reason they're different is because I was toying with different ways. However, the nslog provides this.

( HostMatch, "" ), ( "", HostName )

Which I cannot understand, only makes me thing that the array is initially setup wrong.

Upvotes: 1

Views: 5931

Answers (3)

Indra
Indra

Reputation: 78

    NSArray *array = @[@{@"key1" : @"value1"}, //-index 0-//
                       @{@"key2" : @"value2"}, //-index 1-//
                       @{@"key3" : @"value3"}, //-index 2-//
                       @{@"key4" : @"value4"}]; //-index 3-//

    int i = 0; //-index 0 - 3-//
    NSDictionary *dict = [array objectAtIndex:i];
    NSArray *key = [dict allKeys];
    NSArray *value = [dict allValues];
    NSLog(@"%@", key[0]);
    NSLog(@"%@", value[0]);

result :

2015-08-04 11:46:20.781 key1
2015-08-04 11:46:20.781 value1

Upvotes: 1

Hot Licks
Hot Licks

Reputation: 47699

What you should have is:

NSDictionary *matchInfo = @{@"matchName" : _matchName,
                            @"hostName"  : _hostName};

Upvotes: 3

Richard
Richard

Reputation: 3297

@{@"matchName" : _matchName} this is shorthand for [NSDictionary dictionaryWithObjectsAndKeys:_matchname, @"matchName"]. What this means is that you have an NSArray with two NSDictionaries.

What you probably want is a single NSDictionary with the two keys instead. Like so:

NSDictionary *dictionary = @{@"matchName" : _matchName, @"hostName" : _hostName}

This corresponds to the array( "key" => "value", "key" => "value) type of array from PHP and other languages.

Upvotes: 0

Related Questions