[英]performSegueWithIdentifier gives me NSInvalidArgumentException
[英]Comparing two dictionary values gives me an NSInvalidArgumentException
我有一個for循環,該循環遍歷字典中的字典,試圖找到一個具有與完全獨立的字典中的鍵相匹配的鍵的鍵。
for (id rDCKey in rootDictCopy)
{
tempHouseNumber = rDCKey;
if ([[[rootDictCopy objectForKey:rDCKey] objectForKey:@"RandomUniqueIdentifier"] isEqual:[[routePathRootDictCopy objectForKey:houseNumber] objectForKey:@"RandomUniqueIdentifier"]])
{
NSLog(@"done");
goto DONE;
}
}
當兩個值都等於零時,就可以了,一切都通過了。 但是,一旦它們具有值(通常為256個字符長的NSString),它就會崩潰,並給我這個錯誤:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString objectForKey:]: unrecognized selector sent to instance 0xa000000333939394'
我不知道怎么回事,任何幫助將不勝感激。
如果需要,我可以提供更多代碼。
謝謝。
更新:我更新了for循環以檢查類型,但是發生相同的問題。
更新2:已更改|| 至 &&
for (id rDCKey in rootDictCopy)
{
tempHouseNumber = rDCKey;
if ([[rootDictCopy objectForKey:rDCKey] isKindOfClass:[NSMutableDictionary class]] && [[routePathRootDictCopy objectForKey:houseNumber] isKindOfClass:[NSMutableDictionary class]])
{
if ([[[rootDictCopy objectForKey:rDCKey] objectForKey:@"RandomUniqueIdentifier"] isEqual:[[routePathRootDictCopy objectForKey:houseNumber] objectForKey:@"RandomUniqueIdentifier"]])
{
NSLog(@"done");
goto DONE;
}
}
else
{
NSLog(@"ERROR");
}
}
原因:'-[NSTaggedPointerString objectForKey:]:無法識別的選擇器已發送到實例0xa000000333939394'
發生異常是因為您使用對象類型NSTaggedPointerString
調用了objectForKey
方法
比較之前,您應該檢查數據類型。 您可以執行以下操作:
if ( [obj isKindOfClass:[NSDictionary class]] ) {
// is a NSDictionary
// do further action like get objectForKey, compare ..
} else {
// you don't got what you want -> print error log or something like that
}
您的代碼應如下所示:
for (id rDCKey in rootDictCopy)
{
tempHouseNumber = rDCKey;
// TODO:
// check if rootDictCopy is a NSDictionary (if needed)
// check if routePathRootDictCopy is a NSDictionary (if needed)
// check if [rootDictCopy objectForKey:rDCKey] is a NSDictionary
// check if [routePathRootDictCopy objectForKey:houseNumber] is a NSDictionary
if ([[[rootDictCopy objectForKey:rDCKey] objectForKey:@"RandomUniqueIdentifier"] isEqual:[[routePathRootDictCopy objectForKey:houseNumber] objectForKey:@"RandomUniqueIdentifier"]])
{
NSLog(@"done");
goto DONE;
}
}
注意:我的回答將幫助您的代碼正常運行而不會崩潰,但是您應該在這里找到為什么得到意外的對象類型。 以及如何確保始終刪除NSMutableDictionary
對象,那是最好的解決方案!
正如用戶Nhat Dinh,Amit Tandel和Larme所建議的那樣,rootDictCopy中的某些詞典已在代碼的早期轉換為NSString。 我已修復該問題,使這些詞典仍然是詞典,並且不再收到該錯誤。 感謝大家的幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.