2010-04-12 46 views
0

我正在寫一些代碼得到一些價值當然也包括iPhone問題當然比較值cllocation

-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    //somecode 
    NSString *dirString = [[NSString alloc] initWithFormat:@"%d", newLocation.course]; 
    int myInt = [dirString intValue]; 
    if ((myInt >= 0) || (myint < 90)) {course.text [email protected] "N";} 
    if ((myInt >= 90) || (myint < 180)) {course.text [email protected] "E";} 

等等,但我始終檢索的第一個值,「N」。

我的錯誤在哪裏?

謝謝!

回答

3

可能要改變邏輯OR,以邏輯與(變化||&&),這將確保此值是因爲在0和90,或90和180

之間的邏輯OR,邏輯對我來說似乎也有一點缺陷,也許我對你所做的假設並不瞭解 - 但是如果它的價值是200,那麼它會通過第一個if,因爲200大於0。然後會通過第二個if,因爲200大於90.因爲邏輯OR,它們通過。只有其中一個陳述(> = 0或< 90)必須是真實的才能通過。

這將通過使用邏輯AND來解決。

+0

感謝的jasarien, 謝謝,我已經明白了這個錯誤 – zebra 2010-04-12 15:51:15

1

你不需要通過NSString去檢查課程,但是你錯誤的根本原因是課程是雙重的,你應該在格式化你的字符串時使用%f

短:

 
double theCourse = newLocation.course; 
if ((theCourse >= 0) || (theCourse < 90)) {course.text [email protected] "N";} 
if ((theCourse >= 90) || (theCourse < 180)) {course.text [email protected] "E";} 

但其實我覺得你的算法中是錯誤的。如果課程爲0,則您將向北。< =課程< 45或315 < =課程< 360。

+0

嗨,我試過用你的方式和另一種方式: NSString * courseString = [[NSString alloc] initWithFormat:@「%3.0f」,newLocation.course]; course.text = courseString; 在模擬器中,我試着給courseString賦值並工作。 在我的設備上一次獲得價值應用程序崩潰,我只有移動崩潰報告(我沒有看到任何有用的)。 任何想法?每次我用雙重做這件事時都會發生。 – zebra 2010-04-13 15:31:32

+0

如果你想在你的字符串中的點之後有3個數字,它是「%.3f」 – yonel 2010-04-14 08:19:03

0

@yonel

感謝的,當然是在程度上:-( 但我沒有理解你的算法中,閱讀蘋果文檔我已經發現這一點:

Thus, north is 0 degrees, east is 90degrees, south is180 
degrees, and so on. Course values may not be available on all 
devices. 

對我來說這意味着

course between 0 and 44= North; 
course between 45 and 89= NE; 
course between 90 and 134= East; 
course between 135 and 179= SouthEast; 
course between 180 and 234= South; 
course between 235 and 269= SouthWest; 
course between 270 and 314= West; 
course between 315 and 360 = NorthWest; - 

難道我們說同樣的事情:d