[問題] cs193p 程式問題

看板MacDev作者 (牛牛)時間9年前 (2015/11/11 22:12), 編輯推噓0(005)
留言5則, 1人參與, 最新討論串1/1
https://www.youtube.com/watch?v=ZqKbN_C4Yvg
課程影片約55分處 #import "TextStatsColorAndOutlineViewContrller.h" @interface TextStatsColorAndOutlineViewContrller() @property (weak, nonatomic) IBOutlet UILabel *colorfulCharacterLabel; @property (weak, nonatomic) IBOutlet UILabel *outlineCharacterLabel; @end @implementation TextStatsColorAndOutlineViewContrller -(void)setTextToAnalyze:(NSAttributedString *)textToAnalyze { _textToAnalyze = textToAnalyze; if (self.view.window) [self updateUi];//如果是nil就不出現在螢幕上 } -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self updateUi]; } -(void) updateUi { self.colorfulCharacterLabel.text = [[self characterWithAttribute:NSForegroundColorAttributeName] length]; self.outlineCharacterLabel.text = [[self characterWithAttribute:NSForegroundColorAttributeName] length]; } -(NSAttributedString *)characterWithAttribute:(NSString *)attributeName { NSMutableAttributedString *characters = [[NSMutableAttributedString alloc ] init ]; int index = 0; while (index < [self.textToAnalyze length]) { NSRange range; id value = [self.textToAnalyze attribute:attributeName atIndex:index effectiveRange: &range]; if (value) { [characters appendAttributedString:[self.textToAnalyze attributedSubstringFromRange:range]]; index = range.location + range.length; } else{ index++; } } return characters; } 警告訊息: 在index = range.location + range.length;這一行 implicit conversion loses integer precision 'unsigned long' to 'int' 我的Code都照打 怎麼還是會有warninghttps://www.youtube.com/watch?v=ZqKbN_C4Yvg 課程影片約55分處 #import "TextStatsColorAndOutlineViewContrller.h" @interface TextStatsColorAndOutlineViewContrller() @property (weak, nonatomic) IBOutlet UILabel *colorfulCharacterLabel; @property (weak, nonatomic) IBOutlet UILabel *outlineCharacterLabel; @end @implementation TextStatsColorAndOutlineViewContrller -(void)setTextToAnalyze:(NSAttributedString *)textToAnalyze { _textToAnalyze = textToAnalyze; if (self.view.window) [self updateUi];//如果是nil就不出現在螢幕上 } -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self updateUi]; } -(void) updateUi { self.colorfulCharacterLabel.text = [[self characterWithAttribute:NSForegroundColorAttributeName] length]; self.outlineCharacterLabel.text = [[self characterWithAttribute:NSForegroundColorAttributeName] length]; } -(NSAttributedString *)characterWithAttribute:(NSString *)attributeName { NSMutableAttributedString *characters = [[NSMutableAttributedString alloc ] init ]; int index = 0; while (index < [self.textToAnalyze length]) { NSRange range; id value = [self.textToAnalyze attribute:attributeName atIndex:index effectiveRange: &range]; if (value) { [characters appendAttributedString:[self.textToAnalyze attributedSubstringFromRange:range]]; index = range.location + range.length; } else{ index++; } } return characters; } 警告訊息: 在index = range.location + range.length;這一行 implicit conversion loses integer precision 'unsigned long' to 'int' 我的Code都照打 怎麼還是會有warning.... 不解QQ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.127.186.166 ※ 文章網址: https://www.ptt.cc/bbs/MacDev/M.1447251126.A.5CD.html

11/11 23:12, , 1F
因為編譯的環境不同 arm64的NSInteger是long 之前是int
11/11 23:12, 1F

11/11 23:13, , 2F
啊說錯 之前也是long 不過arm64的long是8byte 其他4byte
11/11 23:13, 2F

11/11 23:17, , 3F
詳細可以參考Apple的document https://goo.gl/t4Oiwe
11/11 23:17, 3F

11/11 23:21, , 4F
32位元 long(4byte) <=> int(4byte) 長度相同 可直接轉換
11/11 23:21, 4F

11/11 23:23, , 5F
64位元 long(8byte) <=> int(4byte) 長度不同 會掉資訊
11/11 23:23, 5F
文章代碼(AID): #1MGqosND (MacDev)
文章代碼(AID): #1MGqosND (MacDev)