iOS: syntax to retrieve value of attribute in array? - ios

I have an array of user objects, one of which is stored in a property _selectedUser. When I log it out, it displays as :
{
uid = 5;
uname = xxxx;
}
However, when I try to access the uid with the following, I get an error:
NSNumber *useridnum = _selectedUser.uid;
Error:
[NSKnownKeysDictionary1 uid]: unrecognized selector sent to instance
What is the proper syntax to retrieve value for uid?

You are trying to access the key as a property on the dictionary, not as a value it contains.
As D.C. suggested in the comments, use this code:
NSNumber *useridnum = _selectedUser[#"uid];
Or:
NSNumber *useridnum = [_selectedUser objectForKey:#"uid"];
You said in the comments that the object is an NSManagedObject. I'm not familiar with Core Data, but it looks like you need to use valueForKey::
NSNumber *useridnum = [_selectedUser valueForKey:#"uid"];
An NSDictionary does not store the keys directly as properties; it stores them in some kind of private data structure. It wouldn't be possible to dynamically store keys as properties without messing with the runtime, and then only a very limited set of strings could be used as keys (instead of basically every possible object), because the keys would have to be C identifiers.
You get this error because when you tried to access the key as a property, the system sent a message to the object saying to call the getter method for the property, but that getter method didn't exist, so the app crashed.

Related

What type of object is returned from ObjectForID

I am trying to retrieve an object from the managed object context and edit a value or two after it has been backed up to the cloud. In particular, I want to save an id provided by the cloud server.
To get the object, I'm trying to retrieve it using its managedObjectID.
I have tried:
Contacts *object = [_managedObjectContext objectWithID:_moID];
and
Contacts *object = [self.managedObjectContext objectRegisteredForID:_moID];
where moID is the managedObject id.
I then follow this up with
object.cid = #99; //ie I set it equal to some number
In both cases, although Contacts is an NSManagedObject class, it throws a yellow warning:
'Incompatible point types initializing contacts with an expression of type NSManagedObject'.
If Instead of calling it Contacts *object, I call it NSManagedObject *object, it no longer throws the warning but then will not allow me to say object.cid as it no longer knows what a cid is. In this case it throws red error:
Property cid not found on object of type NSManagedObject.
Can anyone suggest proper object type. Thanks in advance for any suggestions.
By looking at the method definition, you'll see that returned instance type is just NSManagedObject * as the warning said. What you need to do is just to cast the type appropriately:
Contacts *object = (Contacts *)[_managedObjectContext objectWithID:_moID];
and
Contacts *object = (Contacts *)[self.managedObjectContext objectRegisteredForID:_moID];
Note, your Contacts suppose to be a subclass of NSManagedObject.

NSString from NSString to set as key in NSMutableDictionary

I am getting a initWithObjects:count:]: attempt to insert nil object from objects[0] at the following line:
[contentsOfCocktails setObject:[NSMutableArray arrayWithObject: recipeTitleA] forKey:cocktailsTitleA];
recipeTitleA is the string I'm creating from the cocktails.recipeID class property that equals A. However, I am getting a recipeTitleA equals nil in the debug window.
Here is where I set cocktails.recipeID equal to recipeTitleA:
if ([cocktails.recipeID isEqualToString:#"A"]){
recipeTitleA = cocktails.recipeID;
}
Is this the correct way to set a string equal to another string in order to use it as a key in a NSMutableDictionary?
Long story short: I am trying to extract the recipeIDs that equal A and set them as a key in a dictionary. I will be doing this with other letters as keys as well. I was then going to store them in an array in which I could create sections in the tableview. Data is brought in with FMDB.
I'm new to obj-c and new to data formatting with arrays and dictionaries. Any help would be greatly appreciated!
It looks like you are doing the string comparison correctly. Set a breakpoint at if ([cocktails.recipeID isEqualToString:#"A"]){ and see what you are getting for cocktails.recipeID, if it's nil go back into your cocktails.recipeID property and look if its being set correctly.

NSKeyedArchiver changing from objectForKey to intForKey

I have an app which stores data using NSKeyedArchiver and everything was working fine.
In the first version of the app a value X was stored as a string, but now I changed it to be stored and saved as an int. The problem is if someone updates from the old version to the newest the app crashes because intForKey: gets called on a key containing a String.
Is there some way during decoding to check if what is being decoded is an int or an object?
NSKeyedArchiver doesn't actually encode primitive values. Instead, it wraps them in an NSNumber.
Perhaps, have you tried using decodeObjectForKey: instead of decodeIntegerForKey:?
In example,
id xObject = [decoder decodeObjectForKey:#"xObjectKey"];
self.x = [xObject integerValue];
// I believe this should work because xObject will either by an NSNumber or NSString
// Both of which respond to the selector integerValue

How to extract a value from a key/value pair in NSDictionary (iOS)

When debugging in XCode, the debugger is telling me that the NSDictionary object contains 1 key/value pair. When the debug console prints the description of the key/value pair is shows:
Printing description of testdictionary:
{
"Unknown (<1809>)" = <000000ff>;
}
I want to extract both the <1809> and the <000000ff>. I have tried both the valueForKey and objectforKey methods as described elsewhere on this site. But I think I am having difficulty understanding what is the key and what is the value here.
For example, is "Unknown (<1809>)" the key? Or is "<1809>" the key? Or is 1809 the key?
Thanks Tim for the reply.
The NSDictionary comes from the CoreBluetoothFramework the didDiscoverPeripheral: method is called and passes advertising data into an NSDictionary called "advertisementData".
This dictionary contains all sorts of stuff like the advertising channel and device name. However, I am trying to extract just the advertising data from "advertisementData". I used the key provided by corebluetooth "CBAdvertisementDataServiceDataKey" like this:
NSData* information;
information = [advertisementData objectForKey:CBAdvertisementDataServiceDataKey];
I was declaring "information" as an NSDictionary* object before. But changed it to NSData* after some more reading on Apples documentation. The result is the same. The debugger says that it contains a key/value pair as follows:
"Unknown (<1809>)" = <000000ff>;
Thanks again.
Nik
When you do not know the keys that are present in the dictionary, for example, because the key-value pairs come from an external source, you can use enumerateKeysAndObjectsUsingBlock: method to go through all key-value pairs present in the dictionary:
[testdictionary enumerateKeysAndObjectsUsingBlock::^(id key, id object, BOOL *stop) {
NSLog(#"The key is %#", key);
NSLog(#"The value is %#", object);
}];
I've never seen this before so this is nothing more than an educated guess:
The dictionary may have been casted from CFDictionaryRef, in which case both the key and value are const void * (instead of NSObject). The key might have been some Core Foundation type holding a file descriptor (hence 1809). The value could be a pointer (or an integer casted to a "pointer": (void *)32).
You should try and find out where the dictionary originates from, because it's the only thing that's going to give you any valuable information.
Update: the docs state that the value of CBAdvertisementDataServiceDataKey is a dictionary. The keys are CBUUID objects, representing CBService UUIDs and the values are NSData objects. (1)

iOS RestKit artificial identification attributes

linked RestKit issue #1604
If my API gives me no id attribute, but i still want to cache the objects via Core Data, what should i use to identify my object.
For example i have
response = {
translation = {
text = "longlongtext";
dictionary = "general";
lang = "en";
};
otherdata = {
author = "May";
date = "434134";
};
}
So i would be glad to use hashed (md5) translation text as an id string.
Notice that my future requests which happen without network connection should be able to identify this cached entity and give it as a result.
I cant declare mapping to fill responseID property from [translation.text md5hash] to use as responseMapping.identificationAttributes = #[ responseID ]; because mappings doesnt have such feature.
As proposed by #segiddins in the github issue discussion:
... in your managed object subclass, hook into one of the core data callbacks to generate a compound key that is saved as part of the model and just use that key as your identification attribute.
The approach may look like this:
#property (nonatomic, copy) NSString *identifier;
- (void)willSave
{
[super willSave];
NSString *computedIdentifier = [[NSString stringWithFormat:#"%#%#", self.text, self.langCode] md5hash];
[self setPrimitiveValue:computedIdentifier forKey:#"identifier"];
}
I also wanted to do a hash of the JSON fields like you, but as you know it's not possible. I ended up doing the following to achieve (I believe) the same end result, which is for JSON objects returned without a unique ID, a unique identification attribute is generated by RestKit:
entityMapping.identificationAttributes = #[ #"text",#"dictionary",#"lang",#"author",#"date" ];
You should keep this kind of functionality outside of RestKit if you have no identifiers being provided by the server.
I would generate a custom identifier for each request you make (a GUID), I'd save that identifier into each of the result objects in the RestKit success completion block. I'd also save the request details and the identifier into user defaults.
Now, when the user makes a request and they are offline you can analyse user defaults to determine if it's a repeat request and find the identifier with which to query the results from the data store.
Just to clarify about offline requests after discussion.
In the end, such feature (offline requests) does not exist inside RestKit. The way you can achieve it is complicated, but possible. Steps are:
you use CoreData with RestKit (RKEntityMapping, managed objects etc)
Your provide good identification attributes for entities. It can be URL of request from #metadata.
on both success and failure callbacks from getObjectsAtPath you query CoreData with fetch request and return the result just the same way as if it was loaded directly and taken from mappingResult.firstObject and mark it as cached if it is old result loaded on failure.

Resources