gpt4 book ai didi

ios - 错误 : indexing expression is invalid because subscript type 'char *(*)(const char *, int)' is not an integral or OBjective-C pointer type

转载 作者:行者123 更新时间:2023-11-29 01:13:20 25 4
gpt4 key购买 nike

我有一个简单的drawRandom卡片方法...它将返回指向卡片的指针

-(卡*)drawRandomCard{

Card * randomCard = nil;
if ([self.cards count]){
unsigned index = arc4random_uniform ([self.cards count]);
//return it
randomCard = self.cards[index]; // if I return randomCard I won't get an error!
[self.cards removeObjectAtIndex:index];
}
return self.cards[index];

-如果我这样编写代码,我会收到提到的错误。

-如果我返回 randomCard,则不会收到任何错误。

-我尝试将索引更改为 NSInteger、int、NSUInteger,但没有成功。

-虽然唯一对我有用的是将我的索引定义为一个指针,就像这样@property (nonatomic,assign) NSUInteger index我不明白为什么这有帮助,而且我认为我应该能够按照我想要的方式去做。


我看到了this问题和this问题,但他们的问题是他们的索引不是原始类型,而是一个指针,但我这里的索引是一个 int,所以我仍然很困惑。

感谢任何帮助。

最佳答案

return 语句中的 index 与 if 语句中的 index 不在同一范围内。它是指向全局函数 index() 的指针,它不是有效的下标。

使 index 成为一个属性是可行的,因为它会将它拉出到您的返回可以看到它的范围内。

关于ios - 错误 : indexing expression is invalid because subscript type 'char *(*)(const char *, int)' is not an integral or OBjective-C pointer type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35510637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com