简体   繁体   中英

Is there something wrong return UITextField object like this in ARC mode?

I use ARC in xcode and write the convenience method like this:

    +(UITextField *) addTextField:(NSString *)text x:(int)_x y:(int)_y width:(int)_width height:(int)_height {
       UITextField *v_txt = [[UITextField alloc] init];
       v_txt.frame= CGRectMake(_x, _y, _width, _height);
       v_txt.borderStyle = UITextBorderStyleRoundedRect;
       return v_txt;
}

Many View Controllers call this class method to return the auto released UITextField objects in my application. Is there some memory leaks here? Thanks in advance.

不,在ARC下使用便捷方法时不会发生内存泄漏:编译器足够聪明,可以在最后一个强引用v_txt时处理v_txt发布,并在v_txt时采用自动释放。

It will handle v_txt as autorelease . So no memory leak in your code.

And just for your guidance, please try to follow variable naming conventions .

As most of us will feel _x, _y, _width and _height to be properties of current/self class, but in your case it is parameters to the method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM