B
・Q1. Is the map correct?If you say "right" in the second half of the previous question, "not safe" is the opposite meaning, i.e., if the implementation of Swift changes,It seems that there is no worry that the behavior changes, and it is "correct" Comment https://developer.apple.com/documentation/swift/optional/1539476-map is specified as follows:Affordable closure Optional ,The value of unw.pedSorry, this entry is only available in Japanese.OptionalNo instancenilevaluate the closure. The value is passed as an unwラップped parameter.Also, "map is the purpose of substitute the return value"Indeed, it may be wasteful for non-purpose use, and it may be possible to damage the reach, but it will definitely move and there may be no other means.'What is it?“I don’t do it myself, but if I see someone else writing such a code, it’s quite good.” )Arrays in the old SwiftSequenceHomeforEachinstead because there is no method to saymapThere were people who use it. In that case, I said, “If you do not do it yourself or someone else writes that code, you will be able to make a waste array and throw it away immediately.”This timeOptionalHomemapIn the case, the value made as a side effect in the closure is only typeOptionalBecause it is just wrapped and returned to the type, it can be said that it is not the waste that you can stand out.・Q2. Why Result of call to 'map' is unused Is there a warning?In this case, it is not documented properly to say what kind of warning at any time, so there is a possibility that the warning will appear in the future version, and there are some guesses.First, the declaration of this method suppresses the warning@discardableResultThere is no annotation. In fact, this is a warning (at least in Xcode 10).func setHogeToDict(_ str: String) -> String {
dict["hoge"] = str
return str
}
hoge.map{ setHogeToDict($0) } //! Result of call to 'map' is unused
So,Optional.mapClosure Return Table Passed ToStringIf the type is, the warning will appear. (In this case,mapreturn valueString?Type )No warninghoge.map{ dict["hoge"] = $0 }(Since the Swift substitute does not return the value)Return of closureVoidSince it is a type, in this case it is not to alertIt seems that it is. (In this case,mapreturn valueVoid?Type (for different writing)()?type). )means returning to a clear value,Void typeVoid?There is a time to use the value of the type, but it will be said that it will not be destroyed. In fact,Void?If you try to宣言 a variable that is reasoned to a type, a warning will appear.let result = hoge.map{ dict["hoge"] = $0 }
//! constant 'result' inferred to have type '()?', which may be unexpected
By the way, I wrote that there is a possibility to damage the reedability on top, but if the reedability is a little patience,DictionaryTypeextension extensionHowever, there are people who are disliked to lose their leadership.extension Dictionary {
subscript(ignoreNil key: Key) -> Value? {
get {
return self[key]
}
set {
if let value = newValue {
self[key] = value
}
}
}
} else {let hoge: String? = nil
var dict = [String: Any]()
dict[ignoreNil: "hoge"] = hoge
print(dict) //->[:]
CommentsVoid? Sorry, this entry is only available in Japanese.Void?type empty tuple as value()ornilSince it can be stored, it will not be until the declaration of the variable (including the constant), but you can write this using the value.let hoge: String? = nil
var dict = [String: Any]()
let result: Void? = hoge.map{ dict["hoge"] = $0 } //`Void?`を明記すると警告は出ない
//hogeが非nilの場合`result`の値も非nil(空タプル)になる
if result != nil {
print("dictに値が追加されました")
} else {
print("dictは変更されていません") //->dictは変更されていません
}
It's sorry, "I'm like this!" The code you want to say "(If you use if-let from the beginning, it is easy to read a few steps), but you can say that you can do this.Instead of Optional ChainingmapIt is a kind of technique to use, and it may not be so much if you spread to many people. (Optional Chaining)?.Type of expression usingVoid?There are a lot of examples, but maybe you don’t care much? )