简体   繁体   中英

How To convert [__NSArrayI integerValue] to integer value?

This the line i have using to convert the object to integer values,Inside For Loop I have Placed This code

   NSInteger tag=[[arrFullSubCategory valueForKey:@"category"] integerValue];

Inside arrFullSubCategory:

(
        {
        category = 35;
        image = "images/Hatchback.jpg";
        name = Hatchback;
        parent = 20;
    },
        {
        category = 36;
        image = "images/Sedan.jpg";
        name = Sedan;
        parent = 20;
    },
        {
        category = 37;
        image = "images/SUV.jpg";
        name = SUV;
        parent = 20;
    }
)

Exception:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI integerValue]: unrecognized selector sent to instance 0x7ff4ba58f930'

arrFullSubCategory is an array and you should reach it's elements first. Than you will have NSDictionary objects. After that you can access your category element. So I think your code should be like that:

for (NSInteger i = 0; i < arrFullSubCategory.count; ++i) {
    NSInteger tag=[[[arrFullSubCategory objectAtIndex:i] valueForKey:@"category"] integerValue];
}

Explanation of the error:

The error means you have an array , and arrays don't respond to integerValue .

Your variable arrFullSubCategory references an array (of 3 elements), and each element is a dictionary. If you call valueForKey: on an array of dictionaries then the key lookup is performed for each dictionary and an array is constructed for the results. In your case the result (using literal syntax) is the array:

@[ @35, @36, @37 ]

Whether this array is directly useful to you, or whether you should access the array one element at a time - using a loop or method which calls a block per element, etc. - will depend on what your goal is.

HTH

在for循环中尝试此代码,希望对您有所帮助

NSInteger tag=[[[arrFullSubCategory objectAtIndex:i] valueForKey:@"category"] integerValue];

你有字典数组,所以你用下面的代码

[[[arrFullSubCategory objectAtIndex:] objectForKey:@"category"] integerValue] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM