2

I have JSON response from server, and there is bool variable coming in format 1=true, 0=false.

In my code I do this:

My first try:

NSString *bolean=[dict objectForKey:@"featured"];
    if ([bolean isEqualToString:@"1"]) // here application fails...
    {
        BOOL *a=[bolean boolValue];
        [newPasaka setFeatured:a];
    }
    else
    {
        BOOL *a=[bolean boolValue];
        [newPasaka setFeatured:a];
    }

My second try:

Here it thinks that 1 = NO, 0 = NULL

NSString *bolean=[dict objectForKey:@"featured"];
    if ([bolean boolValue]) //if true (1)
    {
        BOOL *a=[bolean boolValue];
        [newPasaka setFeatured:a];
    }
    else //if false (0)
    {
        BOOL *a=[bolean boolValue];
        [newPasaka setFeatured:a];
    }

How to workaround this?

And my class also handles this crazy.. When i setFeatured to YES - it sets NO. When i set NO - it sets null.

Here is my class:

*.h

@property BOOL *featured;

*.m

@synthesize featured;
Cœur
  • 37,241
  • 25
  • 195
  • 267
Developer
  • 4,158
  • 5
  • 34
  • 66

5 Answers5

4

Change this property FROM -

@property BOOL *featured;

TO

@property (assign) BOOL featured;

BOOL is a primitive data type and can not be created directly as an object. However, if you need to use it as an object, wrap it inside foundation classes like NSNumber or NSString. Like this -

NSNumber *featuredObject = [NSNumber numberWithBool:featured];

And get back the value like this -

BOOL featured = [featuredObject boolValue];

Confused about bool vs BOOL? Read here.

Community
  • 1
  • 1
Ashok
  • 6,224
  • 2
  • 37
  • 55
3

BOOL is not a class, is primitive type

BOOL a = [bolean boolValue];

not

BOOL *a = [bolean boolValue]; // this is wront

Anyway with JSON that value should be rapresented as a Number, not a String, unless the API you are dealing with, force that value to be a string. Put a breakpoint in after the objectForKey and print in console the class of the 'bolean' object:

po [bolean class]

so you will sure about the kind of object you are dealing with, and then in case of a Number (as should be) just use [bolean boolValue]

Manu
  • 788
  • 5
  • 10
0

BOOL is primitive type, so you need not to use pointer with it.

Change this property from

@property BOOL *featured;

to

@property BOOL featured;

Then you need to replace this code:

NSString *bolean=[dict objectForKey:@"featured"];
    if ([bolean isEqualToString:@"1"]) // here application fails...
    {
        BOOL *a=[bolean boolValue];
        [newPasaka setFeatured:a];
    }
    else
    {
        BOOL *a=[bolean boolValue];
        [newPasaka setFeatured:a];
    }

with this:

NSString *bolean=[dict objectForKey:@"featured"];
[newPasaka setFeatured:[boolean boolValue]]

It is greatly simplified and works fine

Alexander Tkachenko
  • 3,221
  • 1
  • 33
  • 47
0

One more way you can do is have modified below:-

@property (assign) Bool featured;
NSString *bolean=[dict objectForKey:@"featured"];
Bool yourBoolValue=[bolean boolValue];
    if (yourBoolValue==1]) // here if (true)
    {
        Bool a=yourBoolValue;
        [newPasaka setFeatured:a];
    }
    else//if (false)
    {
        Bool a=yourBoolValue;
        [newPasaka setFeatured:a];
    }
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
  • `Bool` is not a valid type in Objective-C. Either `bool` or `BOOL`. Also, you don't check for `1` but for `YES` or `true` instead (or not at all, since `if (yourBoolValue)` is sufficient). – DarkDust Oct 24 '13 at 13:50
0

Assuming that dict is the deserialized JSON object, and the boolean value was represented as a JSON number (either 0 or 1), then you obtain a boolean value as follows:

BOOL isFeatured = [dict[@"featured"] boolValue];

Or your code can be written as:

[newPasaka setFeatured:[dict[@"featured"] boolValue]];

Or utilizing Ashok's corrected property declaration:

newPaska.featured = [dict[@"featured"] boolValue];

;)

CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67