标签: nsmutablearray

设置NSMutableArray - 有人可以解释这段代码片段的作用吗?

我在网上找到了这段代码.它以我以前从未见过的方式设置NSMutableArray(我是一个Obj-C newb).有人可以解释它在做什么以及为什么你会这样做?特别是方法签名上的@syncronized,static和little加号.

add the following to the .h file:
+(NSMutableArray *)allMySprites;

add the following to he .m file after implementation:

static NSMutableArray * allMySprites = nil;

+(NSMutableArray *)allMySprites {
    @synchronized(allMySprites) {
        if (allMySprites == nil)
            allMySprites = [[NSMutableArray alloc] init];
        return allMySprites;
    }
 return nil;
Run Code Online (Sandbox Code Playgroud)

}

objective-c nsmutablearray

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

removeAllObjects和NSMutableArray的发布都具有相同的功能吗?

我写了以下代码行:

NSMutableArray *array=[[NSMutableArray alloc]init];
Run Code Online (Sandbox Code Playgroud)

这会分配一些内存.我的问题是,我们怎样才能在以后释放这个内存,无论是使用removeAllObjects方法还是[array release]?

两种方法都具有相同的功能吗?

release objective-c nsmutablearray

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

NSMutableArray addObject不影响计数?

任何人都可以告诉我为什么登录[self.giftees count]仍然返回0,即使我正在添加对象?

标题:

#import <UIKit/UIKit.h>

@interface Test2AppDelegate : NSObject <UIApplicationDelegate>  
{
    UIWindow *window;
    NSMutableArray *giftees;
}

@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) NSMutableArray *giftees;

@end
Run Code Online (Sandbox Code Playgroud)

从didFinishLaunchingWithOptions调用:

- (void)bootstrapGiftees
{
    NSArray *gifteeNames = [NSArray arrayWithObjects:@"Jesse",,nil];

    for (NSString *gifteeName in gifteeNames)
    {
        GifteeModel *g = [[GifteeModel alloc] init];
        g.name = gifteeName;

        [self.giftees addObject:g];
        NSLog(@"giftees count = %d", [self.giftees count]);
        [g release];
}
}
Run Code Online (Sandbox Code Playgroud)

objective-c nsmutablearray

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

僵尸消息[数组计数]

我有一个ivar可变数组,我在viewDidLoad中设置如下:

names = [NSMutableArray arrayWithCapacity:30];
[names addObject:@"Joe"];
[names addObject:@"Dom"];
[names addObject:@"Bob"];
Run Code Online (Sandbox Code Playgroud)

然后在稍后的方法中,点击一个按钮,我执行以下操作,但阵列似乎是过度释放...使用Zombie消息:

int r = arc4random() % [names count];
NSLog(@"%d", r);
Run Code Online (Sandbox Code Playgroud)

我该如何解决?

谢谢.

objective-c nsmutablearray nszombie ios4

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

Objective-C通过NSString迭代获取字符

我有这个功能:

void myFunc(NSString* data) {
    NSMutableArray *instrs = [[NSMutableArray alloc] initWithCapacity:[data length]];
    for (int i=0; i < [data length]; i++) {
        unichar c = [data characterAtIndex:i];
        [instrs addObject:c];
    }
    NSEnumerator *e = [instrs objectEnumerator];
    id inst;
    while (inst = [e nextObject]) {
        NSLog("%i\n", inst);
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为它失败了[instrs addObject:c].它的目的是遍历NSString的十六进制数.导致此代码失败的原因是什么?

iteration nsenumerator objective-c nsmutablearray

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

UITableView reloadData

我有一个包含UITableView的UIViewController.

我正在尝试使用从XML文件解析的数据来填充此表.

TableView第一次加载数据尚不可用.

重新加载此视图的最佳方法是什么?

解析完成后我试着做一个self.TableView reloadData.

不幸的是,在reloadData期间,保持解析结果的NSMutableArray会被重置.

该数组在UIVivewController中设置.

我应该将NSMutablearray设置为全局变量吗?

谢绝了一堆

iphone nsmutablearray

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

如何确定NSMutable Array中最后一个对象的索引号

我有一个NSMutable数组,并试图找到该数组中最后一个对象的索引号.我尝试过这个,但感觉很麻烦:

 int currentCount = [[[self.myLibrary objectAtIndex:currentNoteBookNumber] tabColours] count];
    NSLog(@"Number of tab colours total: %i", currentCount);
NSLog(@"Index number of last object: %i", currentCount-1);
Run Code Online (Sandbox Code Playgroud)

还有另一种方法吗?我的问题的上下文是我需要确定最后一个对象才能更改它:

replaceObjectAtIndex:[last object] withObject: ...
Run Code Online (Sandbox Code Playgroud)

谢谢!

iphone cocoa-touch objective-c nsmutablearray

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

NSMutableArray initwithcapacity值总是为"0"

我的代码如下:

int totalBarCount = 12;
NSMutableArray *tmpValue = [[NSMutableArray alloc]initWithCapacity:totalBarCount];
[tmpValue addObject:[NSNumber numberWithFloat:tmpToplam]];

NSLog (@"%f",[tmpValue count]);
Run Code Online (Sandbox Code Playgroud)

[tmpValue count]总是返回0.为什么?我怎么能得到12?

xcode objective-c nsmutablearray

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

向NSMutableArray属性添加对象会因无法识别的选择器而崩溃?

我得到" - [__ NSArrayI addObject:]:无法识别的选择器发送到实例0x10dd15ee0"这里,当我尝试将addObject添加到NSMutableArray时.我不知道为什么.

我有一组嵌套的自定义类.WTEvent有一个名为mats的NSMutableArray属性,它由WTMat对象组成.mats有一个名为bouts的WTBout对象的NSMutableArray.

我希望能够在比赛中更新一回合,同时单独留下剩下的比赛.在这里,我找到了我需要分配一个回合的垫子.如果没有初始化,我会初始化它.然后,如果它是空的,我尝试添加一个newBout,它崩溃了.

我不应该像这样更改NSMutableArray的元素吗?

// Locate the correct mat
WTMat* mat = [self getMatFromBoutKey:[updateData valueForKey:@"boutKey"]];
WTBout* newBout = [WTBout buildBoutFromDictionary:updateData];

// need to initialize bouts
if ([mat bouts] == nil) {
    NSLog(@"Initializing bouts");
    NSMutableArray* newBouts = [[NSMutableArray alloc] init ];
    [mat setBouts:newBouts];
}

if ([[mat bouts] count]) {
    // if bouts has bouts, then adjust the bout stack

    NSLog(@"bouts is not empty");
} else {
    // if bouts is empty, just add a bout

    NSLog(@"Adding a …
Run Code Online (Sandbox Code Playgroud)

properties objective-c nsmutablearray unrecognized-selector

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

扩展NSMutableArray以像Java数组一样工作

我正在为iphone制作RPG游戏.我想要做的是有一个库存,只允许您持有特定数量的特定类型的项目.我想这样做我只是扩展NSMutableArray并添加限制.我无法弄清楚这样做的最好方法.这是我头脑中的想法......

Backpack.h

@interface Backpack : NSMutableArray {
  Class * arrayClass;
  NSMutableArray * array;
  int limit;
}

-(id) initWithClass:(Class) type andLimit:(int) num;

@end
Run Code Online (Sandbox Code Playgroud)

Backpack.m

@implementation Backpack

  -(id)initWithClass:(Class) type andLimit:(int) num {

    arrayClass = type;
    limit = num;
    array = [NSMutableArray new];
    return self;

  }

  -(void)insertObject:(id) object atIndex:(int) index {

    if([object isKindOfClass:arrayClass] && index < limit) {
      // Insert it
    } else {
      // Throw Exception
    }

  }

@end
Run Code Online (Sandbox Code Playgroud)

arrays objective-c nsmutablearray

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