- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我是 Objective-C 的新手。我已经通读了 a similar question但我不知道如何用这些信息解决我的问题。
基本上,我是这样做的:
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(或任何更高的值)却不行;程序在运行时崩溃(没有错误消息,生成的堆栈转储文件完全空白)。如果重要的话,我正在 Cygwin 中使用 gcc 进行编译。我知道这可能与我上面提到的问题中的保留计数有关,但我不知道如何解决。即使我注释掉最后一行,它也会崩溃……所以它会在 numberWithInt 调用时崩溃,这意味着如果我为 n2 添加一个保留语句,它无论如何都没有机会被调用。
编辑:因为我被要求提供更多代码,这里是我为测试这个问题而制作的文件:
#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;
}
这会打印“1”。然后崩溃,如上所述。这是我的生成文件:
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)
最佳答案
尝试用一行围绕您的代码(您已放置在 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];
关于objective-c - NSNumber numberWithInt 在数字 >= 13 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7165735/
我刚刚开始使用核心数据,遇到了一个问题,我需要使用适当的 ID 保存新对象。 我正在尝试添加行中的下一个数字作为用户添加的对象的 ID: NSFetchRequest *request = [[NSF
我试图在for循环中重现巨大的内存空间,例如,在如下所示的for循环中,如果其中没有自动释放池。这将导致峰值内存占用。 for(int i=0; i< 500000; i++){ NSNumb
我是 Objective-C 的新手。我已经通读了 a similar question但我不知道如何用这些信息解决我的问题。 基本上,我是这样做的: NSMutableArray* array1 =
这可能是一个相当基本的问题,但尽我所能,我找不到导致它的原因。 我编写了以下代码: NSMutableDictionary * myDictionary = [[NSMutableDictio
我今天一直在处理我程序中的漏洞,我理解其中的大部分漏洞,但这个漏洞让我陷入困境: [startAndEndPoints addObject:[NSNumber numberWithInt:button
在《Programming in Objective-C 2.0》一书中的第 326 页上,作者说道: myNumber = [[NSNumber alloc] initWithInt: 1000];
我是一名优秀的程序员,十分优秀!