swift4.1

Why does compactMap return a nil result?

我与影子孤独终老i 提交于 2021-02-08 12:33:09
问题 Consider this code snippet: var a: String? = "abc" var b: String? let result = [a, b].compactMap { $0 } After executing it, result will be ["abc"] which is the expected result. The element of result ( ElementOfResult ) here is String . print(type(of: result)) Array<String> Now to the interesting part. After changing the snippet to var a: String? = "abc" var b: Int? let result = [a, b].compactMap { $0 } and executing it, result will be [Optional("abc"), nil] The element of result (

Swift 4.1 deinitialize and deallocate(capacity:) deprecated

亡梦爱人 提交于 2021-02-07 14:24:03
问题 I've been saying this to form a C array of CGPoint: let arr = UnsafeMutablePointer<CGPoint>.allocate(capacity:4) defer { arr.deinitialize() arr.deallocate(capacity:4) } arr[0] = CGPoint(x:0,y:0) arr[1] = CGPoint(x:50,y:50) arr[2] = CGPoint(x:50,y:50) arr[3] = CGPoint(x:0,y:100) Now (Swift 4.1 in the Xcode 9.3 beta) both deinitialize and deallocate(capacity:) are deprecated. It looks like what I'm supposed to say now might be: defer { arr.deinitialize(count:4) arr.deallocate() } Is that right?

Xcode 9.2 is not showing Swift 4.1

混江龙づ霸主 提交于 2019-12-23 16:08:51
问题 Apple compatibility documentation is mentioning that: Swift 4.1, the default version of Swift that’s included in Xcode 9.2. Yet, using Xcode Version 9.2 (9C40b), the choice of Swift Language Version that I have are 3.2 and 4.0: Is it a mistake in the documentation or a problem in Xcode? Can I use Swift 4.1 with Xcode 9.2? Will I be able to submit to AppStore using it? 回答1: Apple documentation was incorrect, Swift documentation is correct. Well, to be double sure of which version is supported,

Can not conform to protocol by creating extension with Where Clauses

本小妞迷上赌 提交于 2019-12-22 06:42:11
问题 protocol Typographable { func setTypography(_ typography: Typography) } extension UILabel: Typographable {} extension Typographable where Self == UILabel { func setTypography(_ typography: Typography) { self.font = typography.font self.textColor = typography.textColor self.textAlignment = typography.textAlignment self.numberOfLines = typography.numberOfLines } } I have create a protocol Typographable , the UILabel implement this protocol, and the implementation is in the extension

Decodable and JSON, 2 datatypes for same variable

纵饮孤独 提交于 2019-12-21 06:04:46
问题 I'm using the Decodable protocol to decode some json, but I've run into a problem: I'm getting an answer back, where a longitude and a latitide can be either an interger (latitude = 0) if there's no geo location data added to the element, and a String (fx. latitude = "25.047880") if there's geodata available. Now when I decode the json, I don't know how to build my Struct, as the long and lat can't both be String and Int.. So I'm getting a decode error when fetching elements where both cases

Status bar could not find cached time string image. Rendering in-process

天大地大妈咪最大 提交于 2019-12-20 08:21:05
问题 I get the above runtime message after I upgraded to Swift4.1 and Xcode 9.3. Before the upgrade I did not have this message in my console window. Status bar could not find cached time string image. Rendering in-process. comes up every few minutes as long as I have the App running. It sees to me there is no negative side effect, my App is running, as usual, I have not seen any problems. I use the standard Status Bar, have not changed to modify it in any way. Question 1: Would there be a problem

Type 'NSAttributedStringKey' (aka 'NSString') has no member 'foregroundColor'

∥☆過路亽.° 提交于 2019-12-13 04:43:36
问题 Below is line of code for the above mentioned issue. This issue is occurred in Swift 4.1 . let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white] navigationController?.navigationBar.titleTextAttributes = textAttributes 回答1: I've fixed this problem by replacing the Key for Attributed String. Here is my updated Code is: let textAttributes = [NSForegroundColorAttributeName : UIColor.white] navigationController?.navigationBar.titleTextAttributes = textAttributes 来源: https:/

Swift 4.1 Codable/Decodable Nested Array

混江龙づ霸主 提交于 2019-12-08 23:35:34
Need some help with more complicated json, with the newest swift4.1 encoder/decoder: struct: struct LMSRequest: Decodable { let id : Int? let method : String? let params : [String]? enum CodingKeys: String, CodingKey { case id = "id" case method = "method" case params = "params" } init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decodeIfPresent(Int.self, forKey: .id) method = try values.decodeIfPresent(String.self, forKey: .method) params = try values.decodeIfPresent([String].self, forKey: .params) }} json: let json = """ { "id":

Swift 4.1 Codable/Decodable Nested Array

落花浮王杯 提交于 2019-12-08 05:38:05
问题 Need some help with more complicated json, with the newest swift4.1 encoder/decoder: struct: struct LMSRequest: Decodable { let id : Int? let method : String? let params : [String]? enum CodingKeys: String, CodingKey { case id = "id" case method = "method" case params = "params" } init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decodeIfPresent(Int.self, forKey: .id) method = try values.decodeIfPresent(String.self, forKey:

Can not conform to protocol by creating extension with Where Clauses

本小妞迷上赌 提交于 2019-12-05 08:57:13
protocol Typographable { func setTypography(_ typography: Typography) } extension UILabel: Typographable {} extension Typographable where Self == UILabel { func setTypography(_ typography: Typography) { self.font = typography.font self.textColor = typography.textColor self.textAlignment = typography.textAlignment self.numberOfLines = typography.numberOfLines } } I have create a protocol Typographable , the UILabel implement this protocol, and the implementation is in the extension Typographable where Self == UILabel . it works perfectly in swift 4.0, but it not works any more in swift 4.1, the