Swift: Nil is incompatible with return type String
问题 I have this code in Swift: guard let user = username else{ return nil } But I'm getting the following errors: Nil is incompatible with return type String Any of you knows why or how I return nil in this case? I'll really appreciate your help 回答1: you have to tell the compiler that you want to return nil. How do you that? by assigning '?' after your object. For instance take a look at this code: func newFriend(friendDictionary: [String : String]) -> Friend? { guard let name = friendDictionary[