Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSNumber NUMBER与数字冲突>;=13_Objective C_Debugging_Nsnumber - Fatal编程技术网

Objective c NSNumber NUMBER与数字冲突>;=13

Objective c NSNumber NUMBER与数字冲突>;=13,objective-c,debugging,nsnumber,Objective C,Debugging,Nsnumber,我对Objective-C非常陌生。我已经通读了,但我不知道如何用这些信息解决我的问题 基本上,我是这样做的: NSMutableArray* array1 = [[NSMutableArray alloc] initWithCapacity: 1]; NSNumber *n1 = [NSNumber numberWithInt: 12]; [array1 addObject: n1]; NSMutableArray* array2 = [[NSMutableArray alloc] initW

我对Objective-C非常陌生。我已经通读了,但我不知道如何用这些信息解决我的问题

基本上,我是这样做的:

NSMutableArray* array1 = [[NSMutableArray alloc] initWithCapacity: 1];
NSNumber *n1 = [NSNumber numberWithInt: 12];
[array1 addObject: n1];
NSMutableArray* array2 = [[NSMutableArray alloc] initWithCapacity: 1];
NSNumber *n2 = [NSNumber numberWithInt: 13];
[array2 addObject: n2];
将NSNumber 12添加到数组中效果非常好,但添加13(或更高的值)则不行;程序在运行时崩溃(没有错误消息,生成的stackdump文件完全为空)。我在Cygwin用gcc编译,如果这有关系的话。 我知道这可能与保留计数有关,就像我上面提到的问题一样,但我不知道如何修复它。即使我注释掉最后一行,它也会崩溃。。。所以它正好在numberwhithint调用时崩溃,这意味着如果我为n2添加一个retain语句,它无论如何都不会有机会被调用

编辑:因为有人要求我提供更多代码,所以我制作了一个文件来测试这个问题:

#import <stdio.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSValue.h>

int main( int argc, const char *argv[] )
{
    printf("1.\n");
    NSMutableArray* array1 = [[NSMutableArray alloc] initWithCapacity: 1];
    NSNumber *n1 = [NSNumber numberWithInt: 12];
    [array1 addObject: n1];
    NSMutableArray* array2 = [[NSMutableArray alloc] initWithCapacity: 1];
    NSNumber *n2 = [NSNumber numberWithInt: 13];
    [array2 addObject: n2];
    printf("2.\n");

    return 0;
}
#导入
#进口
#进口
int main(int argc,const char*argv[]
{
printf(“1.\n”);
NSMutableArray*array1=[[NSMutableArray alloc]initWithCapacity:1];
NSNumber*n1=[NSNumber numberwhithint:12];
[array1 addObject:n1];
NSMutableArray*array2=[[NSMutableArray alloc]initWithCapacity:1];
NSNumber*n2=[NSNumber numberwhithint:13];
[array2 addObject:n2];
printf(“2.\n”);
返回0;
}
这会打印“1”,然后崩溃,如上所述。这是我的makefile:

CYGWIN_GNUSTEP_PATH=/cygdrive/c/GNUstep
CXX = gcc
MAIN = DummyGame
SOURCES = DummyGame.m
OBJECTS = $(SOURCES:%.m=%.o)
COMP_FLAGS = -std=c99 -I $(CYGWIN_GNUSTEP_PATH)/GNUstep/System/Library/Headers -L $(CYGWIN_GNUSTEP_PATH)/GNUstep/System/Library/Libraries -fconstant-string-class=NSConstantString
LINK_FLAGS = $(COMP_FLAGS) -lobjc -lgnustep-base

all: $(MAIN)

$(MAIN): $(OBJECTS)
    $(CXX) -o $@ $^ $(LINK_FLAGS)

%.o: %.m $(HEADERS)
    $(CXX) -c $< $(COMP_FLAGS)

clean:
$(RM) $(MAIN) $(OBJECTS)
CYGWIN\u GNUSTEP\u PATH=/cygdrive/c/GNUSTEP
CXX=gcc
MAIN=DummyGame
SOURCES=DummyGame.m
对象=$(源:%.m=%.o)
COMP_FLAGS=-std=c99-I$(CYGWIN_-GNUSTEP_路径)/GNUSTEP/System/Library/Headers-L$(CYGWIN_-GNUSTEP_路径)/GNUSTEP/System/Library/Libraries-fconstant字符串类=NSConstantString
LINK_FLAGS=$(COMP_FLAGS)-lobjc-lgnustep base
全部:$(主)
$(主):$(对象)
$(CXX)-o$@$^$(链接标志)
%.o:%.m$(标题)
$(CXX)-c$<$(公司标志)
清洁:
$(RM)$(主)$(对象)
试着用一行代码(您已放置在main中)围绕代码创建一个自动释放池,然后将其排出:

NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init];
NSMutableArray* array1 = [[NSMutableArray alloc] initWithCapacity: 1];
NSNumber *n1 = [NSNumber numberWithInt: 12];
[array1 addObject: n1];
NSMutableArray* array2 = [[NSMutableArray alloc] initWithCapacity: 1];
NSNumber *n2 = [NSNumber numberWithInt: 13];
[array2 addObject: n2];
[pool drain];
请尝试以下操作:

  • 分配这些时,释放
    array1
    array2

    [阵列1释放]; [array2发布]

  • 创建并释放自动释放池:

    NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init]

    [池释放]


  • 在代码的这一部分,您没有做错任何事情。你需要发布更多信息。我在Xcode 4.1中尝试过这个,效果很好。我认为这是一个gcc/Cygwin错误。如果大小合理,请发布您的全部代码。发布一个最小、完整的例子来说明问题。发布您如何编译代码(命令行、Makefile等)。@Jason:Enable
    NSZombies
    查看是否收到错误消息。@Adam-我添加了一个完整的代码示例,演示了这个问题,还有我的Makefile。啊,这就解决了。谢谢小整数成功的原因可能与GNUstep(和Apple)有关。