簡體   English   中英

如何將[__NSArrayI integerValue]轉換為整數值?

[英]How To convert [__NSArrayI integerValue] to integer value?

這行我用來將對象轉換為整數值,在循環內我已放置此代碼

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

內部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;
    }
)

例外:

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

arrFullSubCategory是一個數組,您應該首先訪問它的元素。 比您將擁有NSDictionary對象。 之后,您可以訪問category元素。 所以我認為您的代碼應該是這樣的:

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

錯誤的解釋:

錯誤表示您有一個數組 ,而數組不響應integerValue

您的變量arrFullSubCategory引用了一個數組(包含3個元素),每個元素都是一個字典。 如果在字典數組上調用valueForKey:則將對每個字典執行鍵查找,並為結果構造一個數組。 在您的情況下,結果(使用文字語法)是數組:

@[ @35, @36, @37 ]

此數組是對您直接有用,還是一次使用一個元素調用一個塊的循環或方法,一次訪問一個元素,將取決於您的目標。

高溫超導

在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