How do I get the string before a certain character in swift? The code below is how I did it in Objective C, but can't seem to perform the same task in Swift. Any tips or suggestions on how to achieve this? rangeOfString
seems to not work at all in swift (although Swift has been acting up for me again).
NSRange range = [time rangeOfString:@" "];
NSString *startDate =
[time substringToIndex:range.location];
As you can see from the code above I am able to get the string before the space character in Objective C.
Edit : If I try something like this
var string = "hello Swift"
var range : NSRange = string.rangeOfString("Swift")
I get the following error.
Cannot convert the expression's type 'NSString' to type '(String,
options: NSStringCompareOptions, range: Range?, locale:
NSLocale?)'
Not sure what I did wrong exactly or how to resolve it correctly.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…