"无法识别的选择器发送到实例"对于NSArrayI

时间:2016-02-23 05:32:17

标签: objective-c nsarray uncaught-exception

我有以下代码,它将"无法识别的选择器发送到实例" NSArray的错误。我无法弄清楚这一点,感觉它很简单,我在这里失踪了。如果需要,我可以发布更多内容。

- (NSArray *) getQuotesFromSubId:(NSInteger )subId {

    QuotesAppDelegate *appDelegate = (QuotesAppDelegate *)[[UIApplication sharedApplication] delegate];
    self.quoteMaps = [appDelegate quoteMaps];
    self.quotes = [appDelegate quotes];

    //get the quote_ids from quote_map for this subject_id
    NSString *stringOfSubjectId = [NSString stringWithFormat:@"%ld", (long)subId];

    NSPredicate *filterSubjectId = [NSPredicate predicateWithFormat:@"subject_id == %@", stringOfSubjectId];
    NSArray *quoteMapSection = [self.quoteMaps filteredArrayUsingPredicate:filterSubjectId];
    NSMutableArray *quoteSection = [[NSMutableArray alloc] init];
    NSArray *quoteToAdd = [[NSArray alloc] init];

    for (QuoteMap *qm in quoteMapSection){

        //get the quote_ids from quote_map for this subject_id
        NSPredicate *filter = [NSPredicate predicateWithFormat:@"quote_id == %@", qm.quote_id];

        quoteToAdd = [self.quotes filteredArrayUsingPredicate:filter];
        [quoteSection addObject:quoteToAdd];

    }

    return quoteSection;

}

我认为问题出在这个函数中,它返回一个引号数组:

QuotesAppDelegate *appDelegate = (QuotesAppDelegate *)[[UIApplication sharedApplication] delegate];

NSArray *myQuotes = [appDelegate getQuotesFromSubId:selectedSubject.subject_id];

NSMutableArray *mArray = [appDelegate createMutableArray:myQuotes];
selectedSubject.quotes = mArray;

NSMutableArray *mutableArray = [appDelegate createMutableArray:myQuotes];
selectedSubject.quotes = mutableArray;

这就是我所说的:

2016-02-23 00:24:20.383 Quotes[10631:3698114] -[__NSArrayI excerpt]: unrecognized selector sent to instance 0x15ebbeff0
2016-02-23 00:24:29.164 Quotes[10631:3698114] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI excerpt]: unrecognized selector sent to instance 0x15ebbeff0'
*** First throw call stack:
(0x182b55900 0x1821c3f80 0x182b5c61c 0x182b595b8 0x182a5d68c 0x100078b2c 0x1000642d0 0x187cb17f4 0x187cb1f8c 0x187b9fc90 0x187ba2e88 0x187977284 0x187883394 0x187882e90 0x187882d18 0x185259c00 0x10011dbb0 0x100123658 0x182b0cbb0 0x182b0aa18 0x182a39680 0x183f48088 0x1878b0d90 0x100040398 0x1825da8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException

我收到以下错误

const double myValue = MY_CONSTANT + 1 ? 4.4 : -4.4;

1 个答案:

答案 0 :(得分:1)

您要将-excerpt发送给myQuote的成员(myQuotes)。运行时说NSArrayNSArrayI是内部子类)实例无法理解-excerpt

所以成员的类型是NSArray。我们无法知道,为什么在数组NSArray中有MyQuotes的实例,因为我们没有看到该代码。当您尝试向quotes属性添加新引号并偶然添加整个数组而不是其成员时,可能发生了这种情况。

进行编辑:

这是错误的:

NSArray *quoteToAdd = [[NSArray alloc] init]; // This is an array. It identifier should be quote*s*ToAdd
// BTW: This above code is meaningless, because you do not need to create an array instance. Simply omit "[[NSArray alloc] init]". But this is not your problem.

for (QuoteMap *qm in quoteMapSection){

…
    quoteToAdd = [self.quotes filteredArrayUsingPredicate:filter]; // filtered array returns an *array*
    [quoteSection addObject:quoteToAdd]; // You add the *array* instead of the member of the array.

}

你得到的是一个数组。然后将数组本身(其成员)添加到现有数组中。结果你得到一个包含数组的数组。

简单地改变......

    [quoteSection addObject:quoteToAdd]; 

......来:

    [quoteSection addObjectsFromArray:quoteToAdd];

(并将参考名称更改为复数形式以提高可读性。)