2010-06-02 120 views
1

我仍然在用objective-c找到自己的腳,並想知道是否可以在下面的兩個對象上使用@property。@屬性,在這種情況下可以嗎?

#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 

@interface MapViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate> { 
    CLLocationManager *locationManager; 
    IBOutlet MKMapView *googleMapView; 
} 
@property(nonatomic, retain) CLLocationManager *locationManager; 
@property(nonatomic, retain) MKMapView *googleMapView; 
@end 

我的一個原因,用起來,這樣我可以用我的viewDidUnload的制定者,我似乎可以用@property了很多,只是想知道,如果在這種情況下使用是可以接受的?

-(void)viewDidUnload { 
    [self setLocationManager:nil]; 
    [self setGoogleMapView:nil]; 
    [super viewDidUnload]; 
} 

大加讚賞

加里

回答

3

是。 @property是非常普遍的。你不應該爲使用它而感到驚訝。您的viewDidUnload完全正確。

+0

我可以問一下,如果這兩個變量未被聲明爲@property,viewDidUnload將如何顯示?你會[locationManager發佈]; locationManager = nil;和googleMapView一樣? – fuzzygoat 2010-06-02 15:40:34

+0

@fuzzygoat這可以清理,但你會如何設置它? – hooleyhoop 2010-06-02 15:51:30

+0

我想你將不得不直接設置它們(即)locationManager = [[CLLocationManager alloc] init]; – fuzzygoat 2010-06-02 15:58:32

0

對我來說這似乎完全沒問題。

我能想到的唯一情況是不可接受的,如果這些屬性必須是私人的。 It is a bit trickier to make properties private,但這是隱藏課堂內部不受虐待的基本做法!

1

假設你也使用@synthesize,那麼這正是你應該使用它的地方。不要太掛了有關「非原子」,當然,如果你喜歡

self.locationManager = nil; 
+0

它應該是self.LocationManager = nil;您在使用時會丟失集合。 notation ..;) – 2010-06-02 15:36:51

+0

謝謝@Marcus我糾正了我的答案 – hooleyhoop 2010-06-02 15:39:16

+0

謝謝,我沒有任何反對點符號的內容,但是當我開始學習時,我決定堅持使用括號表示法,直到我更全面地理解語言。 – fuzzygoat 2010-06-02 15:43:15

0

只是注意,你可以使用點語法,但你也可以使用關鍵字IBOutlet財產申報。在這種情況下並不重要,但如果您在合成ivars而不是明確聲明它們,則需要IB的關鍵字自動查看插座。

相關問題