小编fuz*_*oat的帖子

为NSString分配内存?

我是Objective-C的新手,我有点好奇我应该如何管理下面显示的本地NSString变量的内存以及类对象中的相关实例变量.我的代码工作正常,但只是对最佳实践感到好奇.

编辑包含完整的代码,没有什么特别的,就像我说我只是好奇,如果在这个上下文中我应该在NSString对象上进行alloc/release.

// MAIN ------------------------------------------------------------------- **
#import <Foundation/Foundation.h>
#import "PlanetClass.h";

int main (int argc, const char * argv[]) {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString *planet_01_Geek;
    NSString *planet_02_Geek;

    // Create planets
    PlanetClass *newPlanet_01 = [[PlanetClass alloc] init];
    [newPlanet_01 setGeekName:@"StarWars"];
    PlanetClass *newPlanet_02 = [[PlanetClass alloc] init];
    [newPlanet_02 setGeekName:@"Dune"];

    // Query a planet
    planet_01_Geek = [newPlanet_01 geekName];
    planet_02_Geek = [newPlanet_02 geekName];

    // Print details
    NSLog(@"Planet Geek    = %@", planet_01_Geek);
    NSLog(@"Planet Geek    = %@", planet_02_Geek);

    // Clean up
    [newPlanet_01 release]; …
Run Code Online (Sandbox Code Playgroud)

memory cocoa objective-c

2
推荐指数
1
解决办法
3626
查看次数

您能否向NSLog发送保留计数以帮助学习?

只是好奇是否仍然使用NSLog显示对象保留计数.我只想将它们打印到控制台,以帮助了解保留/释放如何在一些简单的代码中工作?

干杯 - 加里 -

memory nslog retain

2
推荐指数
1
解决办法
3883
查看次数

在setter中释放副本的位置?

在我的setter中,我有一个我正在复制的传入字符串,以避免在修改原始文件时出现任何问题.

- (void)setName:(NSString *)newName{
    if(name != newName) {
        [name release];
        name = [newName copy];
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:因为我正在复制我应该在哪里发布它,或者我只是做自动释放?即

- (void)setName:(NSString *)newName{
    if(name != newName) {
        [name release];
        name = [[newName copy] autorelease];
    }
}
Run Code Online (Sandbox Code Playgroud)

加里

cocoa objective-c

2
推荐指数
1
解决办法
676
查看次数

点符号dealloc?

@property (copy) NSString *name;
@property (copy) NSString *orbit;
@property (copy) NSNumber *mass;
@property float surfaceTemp;
@property float rotationSpeed;
Run Code Online (Sandbox Code Playgroud)

目前有这个

- (void)dealloc{
    [name release];
    name = nil;
    [orbit release];
    orbit = nil;
    [mass release];
    mass = nil;
    [super dealloc];
}
Run Code Online (Sandbox Code Playgroud)

如果我使用Dot Notation(Objective-C 2.0)写这个是吗?

- (void)dealloc{
    self.name = nil;
    self.orbit = nil;
    self.mass = nil;
    [super dealloc];
}
Run Code Online (Sandbox Code Playgroud)

加里

cocoa memory-management objective-c

2
推荐指数
1
解决办法
396
查看次数

NSMutableArray,alloc还是capacity?

除了一个返回自动释放的对象而另一个需要手动释放之外,有没有理由选择其中任何一个?

NSMutableDictionary *drink = [[NSMutableDictionary alloc] init];
// do things ...
[drink release];
Run Code Online (Sandbox Code Playgroud)

要么

NSMutableDictionary *drink = [NSMutableDictionary dictionaryWithCapacity:10];
Run Code Online (Sandbox Code Playgroud)

加里

cocoa objective-c

2
推荐指数
1
解决办法
5380
查看次数

<>在类接口中表示/表示什么?

我确定我已经在某个地方读过这个,有谁能告诉我<>在以下界面中代表什么?

@interface GameFinder : NSObject <NSNetServiceBrowserDelegate>
@end
Run Code Online (Sandbox Code Playgroud)

是NSObject采用<NSNetServiceBrowserDelegate>

编辑

有一件事令我困惑......

在我的示例中.界面显示NSNetServiceBrowserDelegate

@interface ITunesFinder : NSObject <NSNetServiceBrowserDelegate>
@end
Run Code Online (Sandbox Code Playgroud)

但是实现显示netServiceBrowser,这些是同一个吗?

@implementation ITunesFinder
-(void) netServiceBrowser: (NSNetServiceBrowser *) browser
           didFindService: (NSNetService *) service
               moreComing: (BOOL) moreComing {
Run Code Online (Sandbox Code Playgroud)

加里

cocoa objective-c

2
推荐指数
2
解决办法
996
查看次数

使用copyItemAtPath复制目录?

我正在尝试将目录"DATA_001"(及其内容)复制到目录"CRYO"中.我的印象是我可以使用copyItemAtPath这样做,就像我对文件一样吗?这是错误的做法吗?

NSString *sourceDir = @"/Users/Fuzzygoat/Documents/DATA_001";
NSString *destDir   = @"/Users/Fuzzygoat/Documents/CRYO";
NSString *sourceFile = @"/Users/Fuzzygoat/Documents/DATA_001/caroline.png";
NSString *destFile   = @"/Users/Fuzzygoat/Documents/CRYO/cjg.png";

// COPY DIR
success = [fileManager copyItemAtPath:sourceDir toPath:destDir error:&dError];
if(success != YES) NSLog(@"Error");

// COPY FILE
success = [fileManager copyItemAtPath:sourceFile toPath:destFile error:&fError];
if(success != YES) NSLog(@"Error");
Run Code Online (Sandbox Code Playgroud)

加里

objective-c

2
推荐指数
1
解决办法
6609
查看次数

iPhone委托和控制器dealloc?

我一直在玩一个简单的iPhone应用程序,并决定在控制器和委托的deallocs中放置一个NSLog语句,但这些都没有打印到Xcode控制台?

// APPLICATION DELEGATE

#import "iPhone_buttonFunAppDelegate.h"
#import "iPhone_buttonFunViewController.h"

@implementation iPhone_buttonFunAppDelegate

@synthesize window;
@synthesize viewController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {       
    // Override point for customization after app launch
    NSLog(@"applicationDidFinishLaunching ...");
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    NSLog(@"AWT:");
}

- (void)dealloc {
    NSLog(@"-DEL-"); // << Does not print?
    [viewController release];
    [window release];
    [super dealloc];
}
@end
Run Code Online (Sandbox Code Playgroud)

//查看控制器

#import "iPhone_buttonFunViewController.h"

@implementation iPhone_buttonFunViewController
@synthesize statusText;

-(IBAction)buttonPressed:(id) sender {
    NSString *title;
    NSString *newText;

    title = [sender titleForState:UIControlStateNormal];
    newText = [[NSString alloc] initWithFormat:@"%@ button …
Run Code Online (Sandbox Code Playgroud)

cocoa-touch objective-c

2
推荐指数
1
解决办法
1159
查看次数

iPhone分配或NSString的字符串文字?

我对以下两个例子有一个简短的问题.目前我正在使用顶级示例,因为iPhone的资源有限,我最好手动分配和释放,因为使用底部字符串文字示例.有没有人喜欢哪一个?

if(activeSegment == 0) {
    NSString *newText = [[NSString alloc] initWithString:@"Hello World"];
    [helloLabel setText:newText];
    [newText release];
}
Run Code Online (Sandbox Code Playgroud)

要么

if(activeSegment == 0) {
    NSString *newText = @"Hello World";
    [helloLabel setText:newText];
}
Run Code Online (Sandbox Code Playgroud)

我个人认为在这种情况下并不重要,因为我在标签上设置文本,直到应用程序退出时才会被释放.

加里

cocoa-touch objective-c

2
推荐指数
2
解决办法
763
查看次数

setValue:forKey:operation?

我很好奇setValue发生了什么:forKey:在下面的代码片段中:它只是将指针设置为指向每个数组,类似于......

[self setMyArray_1: animalArray];
[self setMyArray_2: animalArray];
[self setMyArray_3: animalArray];
Run Code Online (Sandbox Code Playgroud)

另外:setValue:forKey保留数组吗?我猜它确实(如上所​​述)

代码片段:

// INTERFACE
@property(nonatomic, retain) NSArray *myArray_1;
@property(nonatomic, retain) NSArray *myArray_2;
@property(nonatomic, retain) NSArray *myArray_3;

// IMPLEMENTATION
@synthesize myArray_1;
@synthesize myArray_2;
@synthesize myArray_3;

for(counter=1; counter<=3; counter++) {
    NSArray *animalArray = [[NSArray alloc] initWithObjects:@"cat", @"rat", nil];
    NSString *propertyName = [[NSString alloc] initWithFormat:@"myArray_%d", counter];
    [self setValue:animalArray forKey:propertyName];
    [animalArray release];
    [propertyName release];
}
Run Code Online (Sandbox Code Playgroud)

加里

objective-c

2
推荐指数
1
解决办法
6710
查看次数