2010-02-23 13 views
18

Objective-C'de, bir düğmeye her basıldığında bir sayacı artıran basit bir kod bloğum var. Günlüklerim ve hatta arayüzdeki güncellemeler, bir yerine 4'lük bir artış gösteriyor. Bu sadece biçimlendirmemle (% d kullanıyorum) veya eksik olduğum başka bir şeyle ilgili bir sorun mu? Benim tahminim "% d" ile yatıyor ama Objective-C için yeni ve emin değilim. . (Not, ben de denedim "sayaç + = 1;" aynı sonucuObjective-C'de, bu artış neden 1 yerine 4 ile mi?

int counterValue = 0; 
NSLog(@"Count at init: %d",counterValue); 
... 

-(IBAction)pushButton { 
    NSLog(@"Count (Pre-Push) = %d",counterValue); 
    counterValue++; 
    NSLog(@"Count (Post-Push) = %d",counterValue); 
} 

çıkışını aşağıdaki gibidir:

2010-02-20 18:39:39.125 My App[37536:207] Count at init: 0 
2010-02-20 18:39:39.845 My App[37536:207] Count (Pre-Push) = 0 
2010-02-20 18:39:39.846 My App[37536:207] Count (Post-Push) = 4 
2010-02-20 18:39:40.165 My App[37536:207] Count (Pre-Push) = 4 
2010-02-20 18:39:40.166 My App[37536:207] Count (Post-Push) = 8 
2010-02-20 18:39:40.727 My App[37536:207] Count (Pre-Push) = 8 
2010-02-20 18:39:40.728 My App[37536:207] Count (Post-Push) = 12 
+1

Bir nit tarafından tarafındanartım (IBAction) doWhatever Eylem yöntemleri' olmalıdır. – Wevah

+5

@Wevah: gerçekten değil. Tek argüman formu en popüler olsa da, tüm bu yöntem imzaları eylem yöntemleri için eşit olarak geçerlidir: (1) '- (void) eylem', (2)' - (void) eylemi: (id) gönderen ’, (3) '- (void) eylemi: (id) gönderici içinEkran: (UIEvent *) olayı '. Kaynak: http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIControl_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006779-RH2-SW28 –

+0

Ah, haklısınız . Görünüşe göre başka bir UIKit/AppKit farkı. (.S)) .h dosyamda – Wevah

cevap

43

kod bunu gerekmediğine emin gösteriyoruz . Ben doublecheck hızlı bir program yaptı ve ben beklenen sonuçları elde:

2010-02-22 17:04:35.787 app[68267:a0f] Count (Pre-Push) = 0 
2010-02-22 17:04:35.790 app[68267:a0f] Count (Post-Push) = 1 
2010-02-22 17:04:35.923 app[68267:a0f] Count (Pre-Push) = 1 
2010-02-22 17:04:35.924 app[68267:a0f] Count (Post-Push) = 2 

en iyi tahminimyapıyor tip int * başka değişkenin ile counterValue gölgeli olmasıdır Eğer `sender` argümanı kullanarak olmasalar bile, (id) sender`: -: sizeof(int) 1 yerine

+0

için "int * counterValue;" - sorun mu bu? – jbnunn

+0

@jnunn, Yes. O senin problemin. Int türünde bir değişken istiyorsanız, bir işaretçi bildirmeyin. Bu düzeltildi –

+0

- doğru psişik hata ayıklama için teşekkürler Carl – jbnunn

İlgili konular