小编fuz*_*oat的帖子

dealloc,使用release或设置为nil属性?

在实现dealloc时,我在书籍和网络上查看各种代码时都注意到了一点混乱.我的问题是在使用@property时我应该使用以下哪个.到目前为止,我一直在使用VERSION_001.

@property(nonatomic, retain) NSString *name;
@property(nonatomic, retain) NSString *type;
@property(nonatomic, retain) NSString *payload;
@property(nonatomic, retain) NSString *orbit;
Run Code Online (Sandbox Code Playgroud)

版本001

- (void)dealloc {
    [name release];
    [type release];
    [payload release];
    [orbit release];
    [super dealloc];
}
Run Code Online (Sandbox Code Playgroud)

版本002

- (void)dealloc {
    [self setName:nil];
    [self setType:nil];
    [self setPayload:nil];
    [self setOrbit:nil];
    [super dealloc];
}
Run Code Online (Sandbox Code Playgroud)

iphone cocoa-touch objective-c

12
推荐指数
1
解决办法
2049
查看次数

InitWithCoder,[super init]还是[super initWithCoder]?

我可以问我应该使用哪个版本,在我的旧应用程序中,我似乎使用"B"但是当我在网上查看很多示例时,我看到很多版本看起来像"A".

// A
- (id)initWithCoder:(NSCoder *)decoder {
    self=[super initWithCoder:decoder];
    if(self) {
        ...
Run Code Online (Sandbox Code Playgroud)

要么

// B
- (id)initWithCoder:(NSCoder *)decoder {
    self=[super init];
    if(self) {
        ...
Run Code Online (Sandbox Code Playgroud)

iphone cocoa-touch objective-c

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

IBOutlet声明?

我已经看到下面的代码以3种不同的方式编写(关于IBOutlet)是否重要,我想说将IBOutlet添加到声明和@property都更简洁.

只是物业:

@class SwitchViewController;

@interface iPhone_switcherAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    SwitchViewController *switchViewController;
}

@property(nonatomic, retain) IBOutlet UIWindow *window;
@property(nonatomic, retain) IBOutlet SwitchViewController *switchViewController;
@end
Run Code Online (Sandbox Code Playgroud)

只是声明:

@class SwitchViewController;

@interface iPhone_switcherAppDelegate : NSObject <UIApplicationDelegate> {
    IBOutlet UIWindow *window;
    IBOutlet SwitchViewController *switchViewController;
}

@property(nonatomic, retain) UIWindow *window;
@property(nonatomic, retain) SwitchViewController *switchViewController;
@end
Run Code Online (Sandbox Code Playgroud)

都:

@class SwitchViewController;

@interface iPhone_switcherAppDelegate : NSObject <UIApplicationDelegate> {
    IBOutlet UIWindow *window;
    IBOutlet SwitchViewController *switchViewController;
}

@property(nonatomic, retain) IBOutlet UIWindow *window;
@property(nonatomic, retain) IBOutlet SwitchViewController *switchViewController;
@end
Run Code Online (Sandbox Code Playgroud)

欢呼加里

cocoa-touch objective-c

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

pushViewController是否保留了控制器?

我很难找出pushViewController是否保留了控制器,目前我有以下代码(有效)...

ColorController *colorController = [[ColorController alloc] initWithNibName:nibColor bundle:nil];
[[self navigationController] pushViewController:colorController animated:YES];
[colorController release];
Run Code Online (Sandbox Code Playgroud)

但我正在考虑删除发布并添加自动释放...

ColorController *colorController = [[[ColorController alloc] initWithNibName:nibColor bundle:nil] autorelease];
[[self navigationController] pushViewController:colorController animated:YES];
Run Code Online (Sandbox Code Playgroud)

非常感激

加里

iphone cocoa-touch objective-c

11
推荐指数
1
解决办法
2731
查看次数

在哪里记录了[NSIndexPath行]?

任何人都可以指出我在文档中描述[indexPath行]的位置,我看过NSIndexPath但我找不到任何提及"行"的内容?我假设它是一个NSUInteger,但我还想仔细检查它的类型,看看有哪些其他属性可用.

例:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger currentRow = [indexPath row];
}
Run Code Online (Sandbox Code Playgroud)

欢呼加里

iphone cocoa-touch objective-c

11
推荐指数
1
解决办法
5984
查看次数

属性名称上的下划线前缀?

可能重复:
cocoa objective-c类中变量前面的下划线如何工作?

任何人都可以向我指出使用下划线的解释,我一直认为它们用于突出显示您正在访问iVar [_window release];而不是通过setter/getter方法访问iVar,[[self window] release];或者[self.window release];我只是想验证我的理解是正确.

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UILabel *markerLabel;

@synthesize window = _window;
@synthesize markerLabel = _markerLabel;
Run Code Online (Sandbox Code Playgroud)

cocoa-touch objective-c

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

UITableView prepareForSegue为发件人分配indexPath?

在下面的代码我只是尝试直接从设立SEGUE UIViewControllerDetailViewController.我通常从UITableViewCell(我认为更清楚)做segue 但这次我只想尝试直接从ViewController使用-tableView:didSelectRowAtIndexPath:

我的问题是:我遇到了一种情况,我需要UITableViewCell indexPath-prepareForSegue:sender:我的解决方案中访问来自发送indexPath通道sender.通常我会将发件人设置到self这里,我已经环顾四周,我找不到任何对此的引用,我只是好奇如果使用sender传递indexPath是可以接受的吗?我很确定它是,并且我确信我已经读过你可以在某个地方,但我现在找不到任何参考.

// ------------------------------------------------------------------- **
// DELEGATE: UITableView
// ------------------------------------------------------------------- **
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __PRETTY_FUNCTION__);
    [self performSegueWithIdentifier:@"SEGUE_TWO" sender:indexPath];
}

// ------------------------------------------------------------------- **
// SEGUE: ViewController_ONE > DetailViewController
// ------------------------------------------------------------------- **
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSLog(@"%s", __PRETTY_FUNCTION__);
    if([[segue identifier] isEqualToString:@"SEGUE_TWO"]) {
        NSUInteger cellRow = [(NSIndexPath *)sender row];
        DetailViewController *destinationController = [segue destinationViewController];
        [destinationController setDataString:[[self …
Run Code Online (Sandbox Code Playgroud)

iphone cocoa-touch objective-c uitableview segue

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

在initWithCoder中使用retain?

我正在阅读有关编码和解码的内容,我注意到有时人们会错过最后的保留,我也注意到保留有时用在一些可用的而不是其他的.请问 ...

(1)这种保留的目的是什么?为什么有时不需要?

(2)保留的使用是否意味着我需要将其与释放相匹配,如果是这样的话?

- (id) initWithCoder: (NSCoder *) decoder {
    name  = [[decoder decodeObjectForKey: @"CardName"] retain];
    email = [[decoder decodeObjectForKey: @"CardEmail"] retain];
}
Run Code Online (Sandbox Code Playgroud)

要么

- (id) initWithCoder: (NSCoder *) decoder {
    name  = [decoder decodeObjectForKey: @"CardName"];
    email = [decoder decodeObjectForKey: @"CardEmail"];
}
Run Code Online (Sandbox Code Playgroud)

加里

cocoa objective-c

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

在Objective-C中交换Endianness?

我需要交换一些值的字节顺序,只是想知道Objective-C中是否有可用的东西,目前我用下面的CStyle函数交换字节(显然我可以重用)我只是想检查一下没有具体的我失踪?

float floatFlip(float inFloat) {
    union {
        int intValue;
        float newFloat;
    } inData, outData;

    inData.newFloat = inFloat;
    outData.intValue = CFSwapInt32BigToHost(inData.intValue);
    return(outData.newFloat);
}
Run Code Online (Sandbox Code Playgroud)

EDIT_001

感谢这里的指针,我有整数排序,最简单的交换浮点数的方法是什么?

int myInteger = CFSwapInt32BigToHost(myInteger);
Run Code Online (Sandbox Code Playgroud)

(以上代码更新)

float myFloat = floatFlip(myFloat);
Run Code Online (Sandbox Code Playgroud)

加里

objective-c endianness

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

使用[self method]或@selector(method)?

任何人都可以告诉我下面两个陈述之间的差异.

[self playButtonSound];
Run Code Online (Sandbox Code Playgroud)

和:

[self performSelector:@selector(playButtonSound)];
Run Code Online (Sandbox Code Playgroud)

我只是问,因为我有一些使用过的旧代码@selector,现在有了更多的知识,我想不出为什么我没有使用[self playButtonSound],他们似乎都像在这里写的一样.

加里

iphone cocoa-touch objective-c

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

标签 统计

objective-c ×10

cocoa-touch ×8

iphone ×6

cocoa ×1

endianness ×1

segue ×1

uitableview ×1