简体   繁体   English

如何将[__NSArrayI integerValue]转换为整数值?

[英]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: 内部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. arrFullSubCategory是一个数组,您应该首先访问它的元素。 Than you will have NSDictionary objects. 比您将拥有NSDictionary对象。 After that you can access your category element. 之后,您可以访问category元素。 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 . 错误表示您有一个数组 ,而数组不响应integerValue

Your variable arrFullSubCategory references an array (of 3 elements), and each element is a dictionary. 您的变量arrFullSubCategory引用了一个数组(包含3个元素),每个元素都是一个字典。 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. 如果在字典数组上调用valueForKey:则将对每个字典执行键查找,并为结果构造一个数组。 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] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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