swift2.2

How can I get the memory address of a value type or a custom struct in Swift?

╄→гoц情女王★ 提交于 2019-12-04 09:23:23
问题 I'm trying to gain a deeper understanding of how Swift copies value types: The behavior you see in your code will always be as if a copy took place. However, Swift only performs an actual copy behind the scenes when it is absolutely necessary to do so. To advance my understanding, I'd like to get the memory address of a value type. I tried unsafeAddressOf(), but this doesn't work with structs and it seems to cast Swift's standard library types to reference types (e.g. String is cast to

Swift解读专题四——字符串与字符

梦想与她 提交于 2019-12-02 00:45:10
Swift解读专题四——字符串与字符 一、引言 Swift中提供了String类型与Characters类型来处理字符串和字符数据,Swift中的String类型除了提供了许多方便开发者使用的方法外,还可以与Foundation框架的NSString类进行转换,使用起来十分方便。 二、String基础 在Swift中,使用双引号来定义字符串,开发者可以通过如下代码来创建一个字符串常量: let str = "Hello, playground" 可以通过下面两种方式来创建空字符串: let str1 = "" let str2 = String() 调用isEmpty方法可以判断某个字符串是否为空字符串,这个方法将返回一个Bool值,可以直接用于if语句: if str1.isEmpty { print("this String Object is Empty") } 不像Objective-C有NSString与NSMutableString的区别,在Swift中,如果需要创建可变的字符串,只需用变量来接收: var str3 = "Hello" str3 += " "+"World"//str3 = Hello World String也可以使用插值的方法来构造新的字符串,使用\()的方式来将插值的表达式写在小括号内,示例如下: let multiplier = 3 let

No Method declared with Objective-C Selector for Notification UIKeyboardWillShowNotification and UIKeyboardWillHideNotification

若如初见. 提交于 2019-12-01 18:58:45
After the recent update of Xcode, this code that used to work no longer works. Most of the Selector(":") has an auto correction with the exception for this code: override func viewDidLoad() { super.viewDidLoad() NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil); NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil); } which flags an error: No method declared with Objective C selector 'keyboardWillSHow:' This

Type of optionals cannot be inferred correctly in swift 2.2

余生长醉 提交于 2019-12-01 15:05:46
Problem: When running the following code under Xcode 7.3 with swift 2.2, the compiler is unable to correctly infer the type of the optional: import Foundation func whatAmI<T>(inout property:T?) { switch property { case is Int?: print("I am an Int?") case is String?: print("I am a String?") default: print("I don't know what I am") } } var string : String? whatAmI(&string) On my side with Xcode 7.3 this will print I am an Int? However, when I initialize the variable with an empty string before passing it to the function, the switch infers it to be a String?. This would print I am a String? in

Skip item when performing map in Swift?

ⅰ亾dé卋堺 提交于 2019-11-29 18:03:18
问题 I'm applying a map to a dictionary that has a try in it. I'd like to skip the iteration if the mapped item is invalid. For example: func doSomething<T: MyType>() -> [T] dictionaries.map({ try? anotherFunc($0) // Want to keep non-optionals in array, how to skip? }) } In the above sample, if anotherFunc returns nil , how to escape the current iteration and move on to the next? That way, it would not contain the items that are nil . Is this possible? 回答1: Just replace map() by flatMap() :

How to use a variadic closure in swift?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 09:18:37
问题 Is there a proper way to use a variadic parameter list in a closure in Swift? In swift I notice that I can declare a function which takes a variadic argument list like so protocol NumberType: Comparable, IntegerLiteralConvertible, IntegerArithmeticType {} extension Int: NumberType {} extension SequenceType where Generator.Element: NumberType { func satisfy(closure:(args: Generator.Element...) -> ()) { // Do something closure(args: 1, 2, 3) } } Which builds just fine. When I try to use the

Can't form Range with end < start Check range before doing for loop?

和自甴很熟 提交于 2019-11-28 04:28:42
问题 I'm encountering a change in the swift code which i do not quite understand. var arr = [] for var i = 1; i <= arr.count; i += 1 { print("i want to see the i \(i)") } I have a program that gets an result array which can also be empty. This is no problem with the for loop above. Now apple wants me to change the code into the following. But this will crash if the array is empty. var arr = [] for i in 1...arr.count { print("i want to see the i \(i)") } Do I really have to check the range first

How to rewrite Swift ++ operator in ?: ternary operator

荒凉一梦 提交于 2019-11-27 07:49:36
问题 I have the following code var column = 0 column = column >= 2 ? 0 : ++column Since 2.2 I get a depreciation warning, any ideas how I can fix this? I have this solution: if column >= 2 { column = 0 } else { column += 1 } But this isn't really nice. 回答1: How about: column = (column >= 2) ? 0 : column+1 It looks like you might be doing something like clock arithmetic. If so, this gets the point across better: column = (column + 1) % 2 回答2: In the simplest case, you can replace ++column with

Why isn&#39;t [SomeStruct] convertible to [Any]?

China☆狼群 提交于 2019-11-26 10:57:55
Consider the following: struct SomeStruct {} var foo: Any! let bar: SomeStruct = SomeStruct() foo = bar // Compiles as expected var fooArray: [Any] = [] let barArray: [SomeStruct] = [] fooArray = barArray // Does not compile; Cannot assign value of type '[SomeStruct]' to type '[Any]' I've been trying to find the logic behind this, but with no luck. It's worth mentioning if you change the struct to a class, it works perfectly. One could always add a workaround and map each object of the fooArray and cast them to type of Any, but that is not the issue here. I'm looking for an explanation on why

#warning: C-style for statement is deprecated and will be removed in a future version of Swift [duplicate]

醉酒当歌 提交于 2019-11-26 10:34:47
This question already has an answer here: Replacement for C-style loop in Swift 2.2 4 answers I just download a new Xcode (7.3) with swift 2.2. It has a warning: C-style for statement is deprecated and will be removed in a future version of Swift. How can I fix this warning? Removing for init; comparison; increment {} and also remove ++ and -- easily. and use Swift's pretty for-in loop // WARNING: C-style for statement is deprecated and will be removed in a future version of Swift for var i = 1; i <= 10; i += 1 { print("I'm number \(i)") } Swift 2.2: // new swift style works well for i in 1..