我有一个带有计算机AI的游戏,可以将信息保存到数组中,我需要循环遍历数组,这很好但是一旦找到我正在寻找的值,它就会继续搜索,通常这不会是一个问题,但是数组可以变得非常大,我希望能够在我拥有所需的值后简单地停止循环.
您好,我在NSMutableArray上使用performSelector有一个棘手的问题.我有可能误解了performSelector的作用.基本上我有NSMutableArray与对象(所有这些都是基于我的类的对象,它继承自UIButton,但这并不重要,我猜)我需要向数组中的所有对象发送消息.
在我的类的头文件中,我已经声明了函数,它由selector调用.它看起来像这样:
-(void)scaleChangedWithANumber:(NSNumber *)scale;
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我的数组包含3个对象.如果我逐项称呼它们,它的工作原理很完美:
NSNumber *tmpNumber=[NSNumber numberWithFloat:scale];
ButtonOfElement *tmpButton1=[elementsButtonContainer objectAtIndex:0];
[tmpButton1 performSelector:@selector(scaleChangedWithANumber:) withObject:tmpNumber];
ButtonOfElement *tmpButton2=[elementsButtonContainer objectAtIndex:1];
[tmpButton2 performSelector:@selector(scaleChangedWithANumber:) withObject:tmpNumber];
ButtonOfElement *tmpButton3=[elementsButtonContainer objectAtIndex:2];
[tmpButton3 performSelector:@selector(scaleChangedWithANumber:) withObject:tmpNumber];
Run Code Online (Sandbox Code Playgroud)
但是如果我尝试在数组上调用performSelector(我需要向数组中的所有对象发送消息):
NSNumber *tmpNumber=[NSNumber numberWithFloat:scale];
[elementsButtonContainer performSelector:@selector(scaleChangedWithANumber:) withObject:tmpNumber];
tmpNumber=nil;
Run Code Online (Sandbox Code Playgroud)
我收到错误:
[__NSArrayM scaleChangedWithANumber:]: unrecognized selector sent to instance 0x1737f0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM scaleChangedWithANumber:]: unrecognized selector sent to instance 0x1737f0'
*** First throw call stack:
(0x34e456c3 0x370be97f 0x34e49227 0x34e47951 0x34d9f468 0x31d1 0x369a70eb 0x369a7447 0x369324d9 0x368f9c0d 0x36ae5649 0x3681df4f 0x3681c9b3 0x3682791f 0x368274eb …Run Code Online (Sandbox Code Playgroud) 我NSRangeException尝试时遇到错误removeObjectAt:0.我不知道怎么可能.
-(void)removePoints:(ccTime *)tm
{
NSLog(@"naughty %@",_naughtytoucharray);
if ([_naughtytoucharray count]>0)
{
[_naughtytoucharray removeObjectAtIndex:0];
}
}
Run Code Online (Sandbox Code Playgroud)
在日志上显示:
[4195:c07] naughty (
"{124, 98}",
"{123, 98}",
"{135, 97}",
"{124, 98}",
"{148, 94}",
"{135, 97}",
"{157, 93}",
"{148, 94}",
"{162, 92}",
"{157, 93}",
"{164, 92}",
"{162, 92}"
)
然后我收到以下错误:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 11 beyond bounds [0 .. 10]'
当我设置断点时,我看到所有对象都是 0x0

NSMutableArray被分配,初始化init和填写ccTouchesMoved
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { …Run Code Online (Sandbox Code Playgroud) 我有一个自定义类扩展NSObject.我正在维护NSMutableArray这个类对象.这是情况,
customObject-class {
NSString *name;
int ID;
.....and many other properties;
}
customObjectsArray [
customObject1,
customObject2,
...etc
]
Run Code Online (Sandbox Code Playgroud)
现在我尝试使用filterUsingPredicate删除具有nil名称的对象,如下所示,但它返回的对象很少或没有,而我知道有数百个对象的名称不是nil或为空.有人可以告诉我这里可能有什么问题.
[customObjectsArray filterUsingPredicate:[NSPredicate predicateWithFormat:@"name != nil"]];
Run Code Online (Sandbox Code Playgroud) 我有一个NSMutableArray的许多NSDictionaries包含像"标题"这样的键.在某些情况下,有重复的字典具有相同的"标题"但在其他键中有差异.如何删除具有相同"标题"键并且在数组中只留下一个的字典?
谢谢
在我的程序中,我从uint8_t类型的传感器中接收数据。我需要将这些数据存储在NSMutable数组中。
我创建了一个NSMutable数组
NSmutableArray *test;
Run Code Online (Sandbox Code Playgroud)
初始化它
test = [[test alloc]init];
Run Code Online (Sandbox Code Playgroud)
然后我试图将数据存储在我的数组中
[test addObject:Message.data7];
Run Code Online (Sandbox Code Playgroud)
Message.data7为uint8_t格式
但是它想让我这样存储
谁能解释我该怎么做。
提前致谢
我正在尝试编辑在.h文件中声明并在.m文件中合成的NSMutable数组中的对象,但应用程序崩溃与调试说: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent to immutable object'
导致应用程序崩溃的行是:
[noteContent replaceObjectAtIndex:currentNote withObject:noteText.text];
Run Code Online (Sandbox Code Playgroud)
noteContent在.h中声明@property (nonatomic, strong) NSMutableArray* noteContent;,在.m中合成@synthesize noteContent并在viewDidLoad中初始化
noteContent = [[NSMutableArray alloc] init];
noteContent = [standardUserDefaults objectForKey:@"noteContent"];
Run Code Online (Sandbox Code Playgroud)
问题不在于替换nil对象,因为我检查了该位置,存储了一个实际的字符串.
感谢你的付出.
我今天在iOS中发现了一些奇怪的东西,我解决了它,但我想问一下是否有更好的(更多iOS)解决方案.
我想要做的是:
循环遍历字典数组并为iOS中的每个字典添加新密钥
这是代码(这不是我的原始代码,它更复杂,但我完成了这个代码以说明容易点):
- (void)testLoopThruArrayOfDictionaryAndAddNewKeyToEachDictionary
{
NSMutableArray* a = [[NSMutableArray alloc] init];
[a addObjectsFromArray:@[ @{@"one": @11, @"two": @12, } ]];
[a addObjectsFromArray:@[ @{@"one": @21, @"two": @22, } ]];
NSLog(@"%@", a);
/*
// NOT WORKING
for (NSMutableDictionary* dict in a)
{
// -[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0x8b5bca0
[dict setObject:@3 forKey:@"three"];
}
*/
// THIS IS WORKING
for (int i=0; i<[a count]; i++)
{
// make tmp copy
NSMutableDictionary *tmpA = [a[i] mutableCopy];
[tmpA setObject:@3 forKey:@"three"];
// put …Run Code Online (Sandbox Code Playgroud) objective-c nsdictionary nsmutablearray nsarray nsmutabledictionary
我发现由于未捕获的异常“ NSInternalInconsistencyException”而导致的“错误终止”应用程序,原因:“-[__ NSCFArray insertObject:atIndex:]:将变异方法发送给不可变对象”当我第二次将对象添加到可变数组时,第一次成功添加但第二次当它崩溃的应用程序,我认为当我添加对象到数组时有问题。下面的代码是崩溃。
- (void) viewWillAppear:(BOOL)animated
{
if ([defaults objectForKey:@"Groups"] != nil)
{
NSLog(@"not nil defaults.");
arrGroups = (NSMutableArray *)[defaults objectForKey:@"Groups"];
}
else
{
NSLog(@"nil defaults.");
arrGroups = [[NSMutableArray alloc] init];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"button index == %ld",(long)buttonIndex);
//txtCategory = [[alertView textFieldAtIndex:0] text];
if (buttonIndex == 1)
{
//[self addingCategory:self];
NSLog(@"Adding Group name = %@",[[alertView textFieldAtIndex:0]text]);
[arrGroups addObject:[[alertView textFieldAtIndex:0]text]]; **//Crash here! at the time of add second object or also when …Run Code Online (Sandbox Code Playgroud) 在我的项目中,我有一个包含多个字典的数组,每个字典都有一些键值。我想将“链接”键的所有值转换为NSMutableArray。当前包含字典的数组的形式为:
(
{
YoutubeVideo = {
description = "Cervical Spine Medial Branch Nerve Neurolysis neurosurgical animations";
link = "https://player.vimeo.com/video/26186155";
title = "Cervical Spine Medial Branch Nerve Neurolysis neurosurgical animations";
};
},
{
YoutubeVideo = {
description = "What is endoscopic brain surgery";
link = "https://player.vimeo.com/video/16144037";
title = "What is endoscopic brain surgery";
};
},
{
YoutubeVideo = {
description = "Cervical Spine Medial Branch Nerve Block Injection pain management";
link = "https://player.vimeo.com/video/26186167";
title = "Cervical Spine Medial Branch …Run Code Online (Sandbox Code Playgroud) nsmutablearray ×10
ios ×7
objective-c ×6
iphone ×2
nsdictionary ×2
duplicates ×1
for-loop ×1
nsarray ×1
nspredicate ×1
uint8t ×1