Objective-C NSString for loop with characterAtIndex

后端 未结 4 695
逝去的感伤
逝去的感伤 2021-02-04 16:46

I\'m trying to loop through a NSString, character by character, but I\'m getting a EXC_BAD_ACCESS error. Do you have an idea how to do this right? I\'ve been googling for hours

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-04 17:28

    characterAtIndex: returns a unichar, so you should use NSLog(@"%C", ...) instead of @"%@".

    You also cannot use isEqualToString for a unichar, just use == '.' is fine.

    If you want to find the position of all '.'s, you can use rangeOfString. Refer to:

    • String Programming Guide: Searching, Comparing, and Sorting Strings
    • Position of a character in a NSString or NSMutableString

提交回复
热议问题