Strange brackets? - ios

Please anybody now how to remove strange brackets?
Tasting *tasting = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.creationDate.text = [[tasting valueForKey:#"creationDate"] description];
cell.wineName.text = [[[tasting wine]valueForKey:#"wineName"]description];

OK, so the object stored as wineName is an NSSet, which explains why you are getting the value in the form {( "name" )} when calling the description method. Exactly why it's an NSSet object is unknown to me, given a wine a generally marketed under just one name...
To get it in your preferred format don't use description, and instead pull the value out and display like this:
NSSet *wines = [[tasting wine] valueForKey:#"wineName"];
cell.wineName.text = [wines anyObject];
You will also want to do something similar with the date column, using an NSDateFormatter object to format it to the user's preferred format.

Actually wineName here is an NSSet (you can tell that by the {(...)} from the object description and of course by the log of the class) containing your string.
Assuming that your Set will always contain just one object, you could get it like this:
cell.wineName.text = [[[tasting wine] valueForKey:#"wineName"] anyObject];
Though, I do not think that an NSSet is the best option to hold some object's name...
Finally, as a sidenote... using description for presenting data to the user is a bad choice (the method's intended use is for debugging purposes)

Related

How to replace all ordered to-many relationships in NSManagedObject ios

First, please note, I'm aware of the NSOrderedSet bug.
I think the issue I have is that I don't fully understand removeXXX:(NSOrderedSet*)values.
I want to replace and old ordered set with a new ordered set of objects.
What I would have thought is:
[object removeXXX];
[object addXXX:newSet];
So I don't understand the values passed to removeXXX:
Is it possible to merely set the property?:
object.relation = newSet;

objective-c JSON literals, nested - most elegant solution?

Say you do this,
NSString *teste = yourData[#"title"];
no problem if "title" is completely missing in the json: you just get null. If you do this:
NSString *teste = yourData[#"location"][#"city"];
if "city" is missing in the json nest, no problem. if the whole "location" section does not exist, again no problem
However! You'll often see json like this, " largeImage = "<null>"; "
In that case, the app will crash if you are using the code above.
In practice you have to do this:
NSString *imageUrl = nil;
if ([yourResults[thisRow][#"largeImage"] isKindOfClass:[NSDictionary class]])
imageUrl = yourResults[thisRow][#"largeImage"][#"URL"];
My question was really:
is there some dead clever way to perhaps override the literal syntax (ie, override the underlying message, perhaps??) to cover this problem?
So essentially, make this concept [#"blah"] basically first check that indeed it is a dictionary at hand, before trying the operation.
It's a shame because, effectively, you can never use this wonderful syntax
yourData[#"location"][#"city"]
in practice, due to the problem I outline.
PS sorry for the earlier confusion on this question, fixed by Paramag. below - good one Paramag.
Personally i use with JSON category which returns null instead NSNull so my code looks:
[[json objectForKeyNotNull:#"Key"] objectForKeyNotNull:#"Other"]
As you want to have code shorter, i think i would create the category on NSDictionary which could be used as :
[json objectForPath:#"Key.Value"]
Which would expand the path into the keys.
There is some nice gist which looks like it's doing it:
https://gist.github.com/Yulong/229a62c1188c3c024247#file-nsdictionary-beeextension-m-L68
to check this kind of null , you can use valueForKeyPath:
NSString *teste = [CLOUD.yourData[thisRow] valueForKeyPath:#"location.city"];
it will first check for "location" and then for "city".
I would say this is a problem with your schema. null in JSON and a dictionary (called an "object") in JSON are different types of things. The fact that your key can have a value that is sometimes null and sometimes a dictionary seems to me that you guys are not following a rigorous schema.
A good design would have the key either not be there, or if it is there, its value is guaranteed to be a dictionary. In such a case, the Objective-C code NSString *teste = yourData[#"location"][#"city"]; would work without modification because if the key "location" didn't exist, then its value would be nil in Objective-C, and subsequent accesses won't crash, and will also return nil.

Is it possible to assign a new value to a returned pointer?

I have a form that I'm creating and to simplify things, I'm trying to create a form field mapper to an object. As such, I create the following dictionary:
self.fieldPropertyMapper = #{
#(CompanyFieldName):self.company,
#(CompanyFieldDescription):self.company.description,
#(CompanyFieldWebsite):self.company.website,
#(CompanyFieldTwitter):self.company.twitter,
#(CompanyFieldAddress):self.company.address,
#(CompanyFieldAddress2):self.company.address2,
#(CompanyFieldCity):self.company.city,
#(CompanyFieldState):self.company.state,
#(CompanyFieldZipcode):self.company.zipcode,
#(CompanyFieldPhone):self.company.phone
};
The keys here are members of the CompanyFieldType enum.
My goal here is to later in my form to assign a value to the returned pointer. Here's what I mean: when a text field in one of my forms stops editing, I'm looking to set the value. Here's what I'd like to accomplish:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
CompanyFieldType fieldType = [self fieldTypeForTag:textField.tag];
// Set the value of the respective company property
// In theory it would be something like:
// self.fieldPropertyMapper[#(fieldType)] = textField.text;
}
I'm assuming there's a way to assign by reference but I'm forgetting how to do this. (Is it using the & symbol or **?) I don't remember. Help appreciated! If I'm messing up my terminology, feel free to let me know.
You can't do exactly what you want to do. That is to say, there is no pointer magic that will do what you want.
You can get essentially the same effect, though, with key-value coding. Instead of storing the result of accessing the property (e.g. self.company.website), instead you want to just store the key path to the value you're interested in as a string — e.g. #"company.website". Then you can do like so:
[self setValue:textField.text forKey:self.fieldPropertyMapper[textField.tag]];
Using NSMapTable initialized with NSPointerFunctionsStrongMemory for the keys and NSPointerFunctionsOpaqueMemory for the values. Then you could store the addresses of your iVars backing your properties as the values in the table.
[self.mapTable setObject:&_company forKey:#(CompanyFieldName)];
Haven't tested this but this should get you started.

iOS fetchRequest using sort descriptor on modified keys

I have entities with string properties, call it "option" (entity.option is a string). I want to execute a fetch request but I want to compare the float values of "option".
For example, if I have entity1.option = "10" and entity2.option = "5", I want entity2 to be before entity1 after the sort. Sorting by the string values would put entity1 before entity2 since "1" comes before "5".
How can I set up an NSSortDescriptor to do this? I read that compare blocks do not work with fetch requests so can I modify the key used somehow? That is, suppose I create my sortDescriptor via
[NSSortDescriptor sortDescriptorWithKey:ascending:selector:]
Instead of using my property of #"option" as the key, can I do something like #"[option floatValue]"?
EDIT:
I just tried creating a category for NSString that implements a compare method to do what I want. However, even though my code compiles, it fails at run time saying that the compare method I implemented is not a valid selector. I've imported my NSString category into my controller that makes the fetch request but it doesn't work still. I also tried importing the NSString category into my entity that I want to use it but still no luck.
Will this method of using a category not work or am I just not importing the NSString category into the right place?

Get the Type of a Core Data relationship

Is there an easy way to find the object type which forms a specific relationship in Core Data?
For example, I have a one-to-many relationship:
Battery-----1-to-Many-----Comment
If I didn't know that the relationship was for a specific Comment object, is there a programatic way I could find out which object type it is, based solely on the set that I'm dealing with.
Something along the lines of
battery.comments.classType = [Comment class]
I'm aware that both Battery and Comment are of type NSManagedObject - I'd like to know more specifically what they are.
I'm also aware that if the NSSet contains any data, I can use any one of it's objects to query the type. However I need to cater for when there is no data in the NSSet.
Thank you.
You can get all info you need from this few lines:
NSRelationshipDescription* rel = [[[battery entity] relationshipsByName] valueForKey:#"comments"];
NSString* className = [[rel destinationEntity] managedObjectClassName];
NSString* entityName = [[rel destinationEntity] name];
In your AppDelegate you have an NSManagedObjectModel typed property. It has an entities array containing NSEntityDescriptions. From here you should be able to figure it out. Hope this helps!

Resources