daydreamer
daydreamer

Reputation: 91969

Objective-C: How to convert json value to BOOL?

I have a property on my Model as

@property (nonatomic) bool *groupOnly;

and my JSON from API looks like

{
            category =             {
                groupName = Technology;
                id = "b0bddf25-5cce-4184-a589-570fbd39a562";
                name = Software;
            };
            groupOnly = 0;
            recurring = 0;
        }

When I inspect the type of value for the boolean, I see that it is _NSCFBoolean

enter image description here
I want to store this information as boolean, so I tried

budgetCategoryModel.groupOnly = (bool *) [[[json valueForKey:@"groupOnly"] stringValue] isEqual:@"1"];

but what I get back is nil

enter image description here

What am I doing wrong? What is the recommended way to convert to boolean?

Upvotes: 1

Views: 1013

Answers (2)

Henit Nathwani
Henit Nathwani

Reputation: 442

The boolean declaration in your model is incorrect. It should be

@property (nonatomic) BOOL groupOnly;

and the way you should parse the boolean value from you web service response should be like below:

budgetCategoryModel.groupOnly = [[json valueForKey:@"groupOnly"] boolValue];

Hope this helps ..

Upvotes: 1

Ray
Ray

Reputation: 1869

Try changing your model to declare a bool rather than a bool pointer:

@property (nonatomic) bool groupOnly;

Note the lack of a '*'.

Upvotes: 1

Related Questions