标签: nsmutablearray

在nsmutablearray中移动对象

我有一个NSMutableArray;

NSMutableArray
--NSMutableArray
----NSDictionary
----NSDictionary
----NSDictionary
--NSMutableArray
----NSDictionary
----NSDictionary
----NSDictionary
Run Code Online (Sandbox Code Playgroud)

我想将第一个NSDictionary移动到第二个NSMutableArray.这是代码:

id tempObject = [[tableData objectAtIndex:fromSection] objectAtIndex:indexOriginal];
[[tableData objectAtIndex:fromSection] removeObjectAtIndex:indexOriginal];
[[tableData objectAtIndex:toSection] insertObject:tempObject atIndex:indexNew];
Run Code Online (Sandbox Code Playgroud)

它删除了对象,但无法将对象插入新位置.错误是:

[CFDictionary retain]: message sent to deallocated instance 0x4c45110
Run Code Online (Sandbox Code Playgroud)

在头文件中:

NSMutableArray *tableData;
@property (nonatomic, retain) NSMutableArray *tableData;
Run Code Online (Sandbox Code Playgroud)

我如何重新排序/移动nsmutablearray中的对象?

iphone objective-c nsmutablearray

0
推荐指数
1
解决办法
1294
查看次数

将NSMutableArray atIndex(0)复制到字符串变量

我有一个数组,我想在特定的索引提取数组的值,如:

NSString Val1 = Array1[index1];
Run Code Online (Sandbox Code Playgroud)

array是NSMutableArray.

arrays iphone copy objective-c nsmutablearray

0
推荐指数
1
解决办法
142
查看次数

使用ObjectAtIndex可以提供超出边界的错误

我想做一些非常简单的事情,但是我得到了一个错误,我错过了什么?

int i;
int count;

TutorialAppDelegate *appDelegate = (TutorialAppDelegate *)[[UIApplication sharedApplication] delegate];
Animals *aAnimal = (Animals *)[appDelegate.animals objectAtIndex:i];
count = [animals count];


if (i < count)
{
    NSLog(@"%@",aAnimal.animalName);
}
Run Code Online (Sandbox Code Playgroud)

错误:

 '*** -[NSMutableArray objectAtIndex:]: index 22510243 beyond bounds [0 .. 5]'
Run Code Online (Sandbox Code Playgroud)

0 ... 5是正确的!数组中只有6个值.

并使用

NSLog(@"%@",aAnimal.animalName);
Run Code Online (Sandbox Code Playgroud)

out函数返回正确的值,当我也将i更改为0时.

iphone sdk objective-c nsmutablearray

0
推荐指数
1
解决办法
554
查看次数

我是否需要在NSMutableArray中发布对象?

可能重复:
Cocoa内存管理NSArray与对象

我有一个NSMutableArray填充了一些对象.

例如:

...
id test = [NSObject new]; 
NSMutableArray *myArray = [NSMutableArray new];
[myArray addObject: test];
...
Run Code Online (Sandbox Code Playgroud)

当多个对象需要对该数组的引用时,我不能只使用方法" - removeAllObjects".所以我只在我自己的类的" - dealloc"方法中使用数组的发布.

- (void)dealloc {
    [myArray release];
    [super dealloc];
}
Run Code Online (Sandbox Code Playgroud)

那么我的对象名称"test"泄露了,因此我需要做更多的事情吗?

我在文档中找不到答案,对于NSMutableArray的"dealloc"或"release"方法没有意义.在NSObject参考中,他们没有描述NSMutableArray.可能是"内存管理编程指南"中的任何地方(希望如此).

iphone memory-management objective-c nsmutablearray

0
推荐指数
1
解决办法
437
查看次数

如何在NSMutableArray中更改NSString的值?

我有一个可变的字符串数组.对于这个例子,假设其中有2个字符串.

我想要做的操作是获取第一个字符串并将第二个字符串的值赋给它.

我正在尝试这样的事情:

- (void) someAction {
    NSMutableArray * array = [[NSMutableArray alloc] initWithObjects: string1, string2, nil];
    NSString * firstString = [array objectAtIndex:0];
    NSString * secondString = [array objectAtIndex:1];
    firstString = secondString;
}
Run Code Online (Sandbox Code Playgroud)

但这种方法似乎不起作用.在我记录这两个字符串后,它们在操作后不会改变.

请指教.

objective-c nsstring nsmutablearray ios

0
推荐指数
1
解决办法
2057
查看次数

将ABRecordRef添加到NSMutableArray会导致需要桥接转换的错误

我收到一个错误:

将C指针类型'ABRecordRef'(又名'const void*')隐式转换为Objective-C指针类型'id'需要桥接转换

从这段代码,尝试添加ABRecordRef一个NSMutableArray

ABRecordRef person = (__bridge ABRecordRef)[contactArr objectAtIndex:i];
[addressBookArray addObject:person];
Run Code Online (Sandbox Code Playgroud)

addressBookArray 被定义为

NSMutableArray *addressBookArray;
Run Code Online (Sandbox Code Playgroud)

cocoa-touch objective-c nsmutablearray abrecord

0
推荐指数
1
解决办法
1112
查看次数

Objective-c:将自定义对象添加到NSMutableArray

我通常使用java或c ++编程,最近我开始使用objective-c.在objective-c中寻找向量,我发现NSMutableArray似乎是最好的选择.我正在做一个opengl游戏,我正在尝试为我的精灵创建一个NSMutableArray纹理四边形.这是相关代码:

我定义纹理四边形:

typedef struct {
    CGPoint geometryVertex;
    CGPoint textureVertex;
} TexturedVertex;

typedef struct {
    TexturedVertex bl;
    TexturedVertex br;    
    TexturedVertex tl;
    TexturedVertex tr;    
} TexturedQuad;
Run Code Online (Sandbox Code Playgroud)

我在界面中创建了一个数组:

@interface Sprite() {
    NSMutableArray *quads;
}
Run Code Online (Sandbox Code Playgroud)

我启动数组,我创建基于"宽度"和"高度"的texturesQuads,它们是单个精灵的尺寸,"self.textureInfo.width"和"self.textureInfo.height",它们是维度整个精灵表:

    quads = [NSMutableArray arrayWithCapacity:1];
    for(int x = 0; x < self.textureInfo.width/width; x++) {
    for(int y = 0; y < self.textureInfo.height/height; y++) {
        TexturedQuad q;
        q.bl.geometryVertex = CGPointMake(0, 0);
        q.br.geometryVertex = CGPointMake(width, 0);
        q.tl.geometryVertex = CGPointMake(0, height);
        q.tr.geometryVertex = CGPointMake(width, height);

        int x0 = (x*width)/self.textureInfo.width;
        int x1 = …
Run Code Online (Sandbox Code Playgroud)

struct objective-c nsmutablearray

0
推荐指数
1
解决办法
567
查看次数

NSMutable数组removeAllObject也删除其他NSMutable数组中的对象

我有三个类,即DepartmentViewController,DepartmentRequest,Department.就像我从DepartmentViewcontroller发出部门请求并基于响应我操纵repsonse并在DepartmentRequest类中添加NSmutableArray *responseArray.该数组只包含Department对象.我希望在DepartmentViewController中使用此responseArray来搜索和重新加载tableview.所以我通过委托将此数组传递给DepartmentViewController,并将responseArray分配给localArray.现在我根据这两个数组进行了搜索,但是如果我使用removeallobject删除任何一个数组.它也在其他Array中删除对象.

if(searchString.length>0)
  {

      [localArray removeAllObjects];
    for (int i =0 ; i < [departmentRequest.responseArray count]; i++) {
      Department *dept = [departmentRequest.responseArray objectAtIndex:i];

        if (([dept.departmentName rangeOfString:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)].location != NSNotFound)
          )

        {
                   [localArray addObject:dept];
        }
        else{
            NSLog(@"deptname %@",dept.departmentName);
        }

    }

    [departmentTableView reloadData];
Run Code Online (Sandbox Code Playgroud)

如果我删除localArray中的对象,则删除departmentReqeust.responseArray和localArray中的对象

arrays objective-c nsmutablearray ios

0
推荐指数
1
解决办法
207
查看次数

NSArray的对象"可能"为零

我有3个对象可能以随机顺序初始化.

所以,如果对象"objectOne",objectTwo","objectThree"按此顺序初始化

myArray  = [NSArray arrayWithObjects:objectOne,objectTwo,objectThree nil];
Run Code Online (Sandbox Code Playgroud)

所有对象都没有问题进入数组,但在我的情况下objectOne,objectTwo可能是nil而objectThree可能不是nil,在这种情况下我希望myArray返回(count)1.

如果objectOne为nil但objectTwo和objectThree不为nil,我希望我的数组返回(count)2.

在最后两种情况下,我的数组总是返回nil.最好的办法是什么?

objective-c nsmutablearray nsarray ios

0
推荐指数
1
解决办法
566
查看次数

创建NSDictionary的NSMutalbeArray时的EXC_BAD_ACCESS

我正在尝试填充一个字典数组.但是xcode说EXC_BAD_ACCESS

这是一项基本操作.错误在哪里?

@interface MenuViewController ()
@property (nonatomic, strong) NSMutableArray *items;
@end

@implementation MenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.items = [[NSMutableArray alloc]initWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title","MenuHomeIcon.png",@"image", nil],
                                                        [NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title","MenuFindIcon.png",@"image", nil],
                                                        [NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title","MenuProfileIcon.png",@"image", nil],
                                                        [NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title","MenuSettingsIcon.png",@"image", nil],nil];

}
Run Code Online (Sandbox Code Playgroud)

我尝试了另一种方式,但结果是一样的:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.items = [[NSMutableArray alloc]initWithCapacity:4];

    [self.items addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title","MenuHomeIcon.png",@"image", nil]];
    [self.items addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title","MenuFindIcon.png",@"image", nil]];
    [self.items addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title","MenuProfileIcon.png",@"image", nil]];
    [self.items addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title","MenuSettingsIcon.png",@"image", nil]];

}
Run Code Online (Sandbox Code Playgroud)

objective-c nsdictionary nsmutablearray ios

0
推荐指数
1
解决办法
60
查看次数