iphonesdk:返回不带alloc的UILabel的正确方法

iphonesdk:返回不带alloc的UILabel的正确方法,iphone,uilabel,Iphone,Uilabel,我目前有一些代码如下所示: -(UIView)someMethod { CGRectMake(0,0,100,100); UILabel *label = [[UILabel alloc] initWithFrame:rect]; return label; } 当它工作时,它显然会泄漏内存,需要修复。我想解决办法是: UILabel *label = [UILabel initWithFrame:rect]; 但是编译器告诉我UILabel不响应initWithFrame。我想

我目前有一些代码如下所示:

-(UIView)someMethod {
  CGRectMake(0,0,100,100);
  UILabel *label = [[UILabel alloc] initWithFrame:rect];
  return label;
}
当它工作时,它显然会泄漏内存,需要修复。我想解决办法是:

UILabel *label = [UILabel initWithFrame:rect];
但是编译器告诉我UILabel不响应initWithFrame。我想我的问题有两个:

a) 正确的方法是什么,这样我就不会泄漏内存

b) 我不明白为什么[UILabel alloc]会响应initWithFrame而不是UILabel本身(我的理解是UILabel是从UIView继承的,UIView会响应initWithFrame)。

a)您无法避免
+alloc
。但您可以使用
-autorelease
放弃所有权

-(UIView*)someMethod {
  CGRect rect = CGRectMake(0,0,100,100);
  UILabel *label = [[[UILabel alloc] initWithFrame:rect] autorelease];
  return label;
}
b)
+alloc
是类方法,
-initWithFrame:
是实例方法。后者只能在UILabel实例上调用(或者,用ObjC术语,发送到)。但是,符号“UILabel”是一个类,而不是一个实例,因此
[UILabel initWithFrame:rect]
不起作用。类似地,像
+alloc
这样的类方法只能在类上调用,因此
[label alloc]
将不起作用。

a)您无法避免
+alloc
。但您可以使用
-autorelease
放弃所有权

-(UIView*)someMethod {
  CGRect rect = CGRectMake(0,0,100,100);
  UILabel *label = [[[UILabel alloc] initWithFrame:rect] autorelease];
  return label;
}
b)
+alloc
是类方法,
-initWithFrame:
是实例方法。后者只能在UILabel实例上调用(或者,用ObjC术语,发送到)。但是,符号“UILabel”是一个类,而不是一个实例,因此
[UILabel initWithFrame:rect]
不起作用。类似地,像
+alloc
这样的类方法只能在类上调用,因此
[label alloc]
将不起作用。

可能更像:

-(UILabel)someMethod {

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,100)];
    return [label autorelease];
    }
也许更像:

-(UILabel)someMethod {

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,100)];
    return [label autorelease];
    }

只需使用您原来的方法并使用

return [label autorelease];

相反。

只需使用您原来的方法并使用

return [label autorelease];
相反。

a)

b) 您误解了类方法和实例方法之间的区别

类方法的声明和使用方式如下:

// declaration: notated with +
+ (NSDocumentController *)sharedDocumentController;
// usage
NSDocumentController * thang = [NSDocumentController sharedDocumentController];
// declaration: notated with -
- (id)init;
// usage:
// + alloc is a class method
//     this requests the class (NSObject) to allocate and return a newly allocated NSObject
// - init is the instance method
//     this sends a message to the NSObject instance (returned by [NSObject alloc])
NSObject * thang = [[NSObject alloc] init];
实例方法的声明和使用方式如下:

// declaration: notated with +
+ (NSDocumentController *)sharedDocumentController;
// usage
NSDocumentController * thang = [NSDocumentController sharedDocumentController];
// declaration: notated with -
- (id)init;
// usage:
// + alloc is a class method
//     this requests the class (NSObject) to allocate and return a newly allocated NSObject
// - init is the instance method
//     this sends a message to the NSObject instance (returned by [NSObject alloc])
NSObject * thang = [[NSObject alloc] init];
在您的示例中,
alloc
返回一个分配的实例,然后您需要在该实例上调用适当的init instance方法

有些类提供了方便的构造函数,这些构造函数通常返回自动释放的实例:

+ (NSNumber *)numberWithInt:(int)anInt;
但是,以这种方式复制代码并不常见,除非您正在处理诸如类集群之类的高级主题。当然,如果您发现您经常需要特定的功能或方便的构造函数,那么将其添加到接口中可能是一个好主意。

a)

b) 您误解了类方法和实例方法之间的区别

类方法的声明和使用方式如下:

// declaration: notated with +
+ (NSDocumentController *)sharedDocumentController;
// usage
NSDocumentController * thang = [NSDocumentController sharedDocumentController];
// declaration: notated with -
- (id)init;
// usage:
// + alloc is a class method
//     this requests the class (NSObject) to allocate and return a newly allocated NSObject
// - init is the instance method
//     this sends a message to the NSObject instance (returned by [NSObject alloc])
NSObject * thang = [[NSObject alloc] init];
实例方法的声明和使用方式如下:

// declaration: notated with +
+ (NSDocumentController *)sharedDocumentController;
// usage
NSDocumentController * thang = [NSDocumentController sharedDocumentController];
// declaration: notated with -
- (id)init;
// usage:
// + alloc is a class method
//     this requests the class (NSObject) to allocate and return a newly allocated NSObject
// - init is the instance method
//     this sends a message to the NSObject instance (returned by [NSObject alloc])
NSObject * thang = [[NSObject alloc] init];
在您的示例中,
alloc
返回一个分配的实例,然后您需要在该实例上调用适当的init instance方法

有些类提供了方便的构造函数,这些构造函数通常返回自动释放的实例:

+ (NSNumber *)numberWithInt:(int)anInt;

但是,以这种方式复制代码并不常见,除非您正在处理诸如类集群之类的高级主题。当然,如果您发现您经常需要特定的功能或方便的构造函数,那么将其添加到接口中可能是一个好主意。

但是返回类型是(UIView*),函数返回(UILabel)!!!可能吗?@Maulik:UILabel是UIView的子类。但是返回类型是(UIView*),函数返回(UILabel)!!!可能吗?@Maulik:UILabel是UIView的一个子类。