Re: [swift-users] Strong Reference Cycle

2016-07-02 Thread Zhao Xin via swift-users
Bar() will be released as soon as start(:)'s finishes execution. As there is no strong reference to Bar() and the closure runs inside of function start(:). steps: Bar() created execute Bar().start(:) with {print(self.object)} pass closure {print(self.object)} to start(:) closure runs inside of fu

Re: [swift-users] lazy initialisation

2016-07-04 Thread Zhao Xin via swift-users
You'd better sharing some of you code here first. Zhaoxin On Tue, Jul 5, 2016 at 1:04 AM, J.E. Schotsman via swift-users < swift-users@swift.org> wrote: > Hello, > > I need to initialize a variable of a class with a closure using the value > of some variables of the class. > Since this is not pe

Re: [swift-users] lazy initialisation

2016-07-04 Thread Zhao Xin via swift-users
No, it is not a bug. For a closure, you have to call self explicitly unless the closure is mark as @noescape. Also, in this situation, self is not unowned, as the closure is not stored, it ran and released. Below, is a situation that you need use unowned self. Here the closure is stored in variabl

Re: [swift-users] Expression pattern cannot be a named tuple constant

2016-07-04 Thread Zhao Xin via swift-users
I think switch treats (x,y) as two variables instead of a tuple. So it prohibits int_1_1 as it looks like one value only at the first glance. Below two expressions will work. case (int_1_1.0,int_1_1.1):// case let foo where foo == int_1_1:// ​That why you can use someth

Re: [swift-users] Cannot invoke 'stride' with an argument list of type '(from: Int, to: Int, by: Int)'

2016-07-04 Thread Zhao Xin via swift-users
In Swift 3, func stride>( from start: T, to end: T, by stride: T.Stride) -> StrideTo Int does not conform to Strideable. Adopted By CGFloat Decimal Double Float Float80 String.UTF16View.Index UnsafeMutablePointer UnsafePointer ​In Swift 2.2, @warn_unused_result func stride(to *end*: Self, by

Re: [swift-users] Cannot invoke 'stride' with an argument list of type '(from: Int, to: Int, by: Int)'

2016-07-05 Thread Zhao Xin via swift-users
ckson wrote: > Int conforms to Strideable byway of Integer <- SignedInteger <- Int (not > exactly sure how it will be once the integer proposal is implemented but it > will still be strideable). > > -Shawn > > On Mon, Jul 4, 2016 at 10:38 PM Zhao Xin via swift-users

Re: [swift-users] [Bug or ByDesign?] unowned optional values not allowed

2016-07-05 Thread Zhao Xin via swift-users
According to you description, you should use weak var parent:A! Zhaoxin On Mon, Jul 4, 2016 at 2:29 PM, Karl via swift-users wrote: > Currently it’s not possible to have an unowned optional value. E.g: > > class A { > unowned var parent : A? // ‘unowned’ may only be applied to >

Re: [swift-users] another issue with tuples

2016-07-05 Thread Zhao Xin via swift-users
Tuple is not designed to be used as commonly as classes and structs. NOTE > Tuples are useful for temporary groups of related values. They are not > suited to the creation of complex data structures. If your data structure > is likely to persist beyond a temporary scope, model it as a class or > s

Re: [swift-users] Why does RangeReplaceableCollection require an empty initialiser?

2016-07-06 Thread Zhao Xin via swift-users
According to the document of Swift 3, Array has already conformed protocol RangeReplaceableCollection. Zhaoxin On Wed, Jul 6, 2016 at 7:09 PM, Tim Vermeulen via swift-users < swift-users@swift.org> wrote: > RangeReplaceableCollection has three initialisers: init(), init(_:) and > init(repeating:

Re: [swift-users] Why does RangeReplaceableCollection require an empty initialiser?

2016-07-06 Thread Zhao Xin via swift-users
Then how you defined the index to conform to Strideable? Below code does work as it seams that you can't use generics in subscripts. subscript(index:T) -> Element Zhaoxin On Wed, Jul 6, 2016 at 8:32 PM, Tim Vermeulen wrote: > > On 6 Jul 2016, at 14:03, Zhao Xin wrote: > > According to the do

Re: [swift-users] Why does RangeReplaceableCollection require an empty initialiser?

2016-07-06 Thread Zhao Xin via swift-users
Now I understood you concerns. Have you ever thought of if a non-empty RangeReplaceableCollection being removed all of its elements, which makes the collection to be an empty collection. That shouldn't change the RangeReplaceableCollection to be a non-RangeReplaceableCollection. So the empty collec

Re: [swift-users] Why does RangeReplaceableCollection require an empty initialiser?

2016-07-06 Thread Zhao Xin via swift-users
N ​o. You didn't catch what I meant. I meant it should be like an equation. ​If foo is a ​RangeReplaceableCollection, ​ foo minus foo equates zero, zero means an empty collection. Both side of the equation should be with the same unit, the unit is RangeReplaceableCollection. ​ Below code also sho

Re: [swift-users] Why does RangeReplaceableCollection require an empty initialiser?

2016-07-06 Thread Zhao Xin via swift-users
I am not a software scientist. I have to explain things with examples. For example, in Framework headers. extension Array : RangeReplaceableCollection { > /// Creates a new, empty array. > /// > /// This is equivalent to initializing with an empty array literal. > /// For example:

Re: [swift-users] lazy initialisation

2016-07-08 Thread Zhao Xin via swift-users
The compiler is not smart enough to treat this as you think, nor it will be designed to. According to the documents, it is the developer‘s burden ​to add @noescape or weak or unowned. So I disagree it is a bug. Zhaoxin On Sat, Jul 9, 2016 at 7:30 AM, Karl wrote: > > On 5 Jul 2016, at 03:47, Zha

Re: [swift-users] access violation when weak variable

2016-07-09 Thread Zhao Xin via swift-users
You code works fine in Xcode 7.3.1 (7D1014). So it must be a bug in Xcode 8. Zhaoxin On Sat, Jul 9, 2016 at 10:18 PM, Ray Fix via swift-users < swift-users@swift.org> wrote: > > Hi! > > When I make a variable weak in Xcode 8 (both beta 1 and beta 2) I get a > access violation. I think this is a

Re: [swift-users] Why there's no Character literals?

2016-07-10 Thread Zhao Xin via swift-users
Of course not. Struct Set is commonly known. let foo:Set = [1,2,3,4] let bar:Set = [1,2,3,4] If you do not specify them as Sets, foo and bar will be Arrays. Zhaoxin On Thu, Jul 7, 2016 at 5:35 PM, 王 黎明 wrote: > > In Swift, we must specify the type for Character variables(because there’s > n

Re: [swift-users] Difference between Apple and Linux versions of Swift - CompareOptions

2016-07-10 Thread Zhao Xin via swift-users
I think they should be the same when Swift 3 release. You example is just how API will change from Swift 2 to 3. You can file a bug if you want to speed up the changing. Zhaoxin On Mon, Jul 11, 2016 at 6:13 AM, Jon Hoffman via swift-users < swift-users@swift.org> wrote: > With Swift 3.0 Preview

Re: [swift-users] Syntax for extending a struct conforming to a protocol with constraint

2016-07-10 Thread Zhao Xin via swift-users
How about this? struct Polynomial { > //definition goes here > } > extension Polynomial: CustomStringConvertible { > //implementation goes here > } Zhaoxin On Sun, Jul 10, 2016 at 1:54 PM, 褚晓敏 via swift-users wrote: > Hello, every one. I want to extend a struct to conform to

Re: [swift-users] Difference between Apple and Linux versions of Swift - CompareOptions

2016-07-10 Thread Zhao Xin via swift-users
You are right, the shorter one will exist in the final release. Always the shorter ones. Zhaoxin On Mon, Jul 11, 2016 at 9:50 AM, Jon Hoffman wrote: > Thank you for the reply. I am pretty sure that you are correct about them > being the same when Swift 3 is released. I am more concerned about

Re: [swift-users] UITextField issue

2016-07-11 Thread Zhao Xin via swift-users
​This mailing list focuses on Swift and its own libraries only​, not on UIKit nor Cocoa Touch. Please ask you question on Apple Developer Forum or StackOverFlow instead. Zhaoxin On Tue, Jul 12, 2016 at 10:01 AM, 陈立红 via swift-users wrote: > I am now developing an ios app. > And I found it the u

Re: [swift-users] didSet and time when propagation of mutation happens

2016-07-13 Thread Zhao Xin via swift-users
I think the order is right. value.set >> value.didSet >> string.set >> string.didSet you expected value.set >> string.set >> value.didSet >> string.didSet is not correct. The value "1" is not you expected. However, that is something that I think is tricky. For closure > { > print(myClass.

Re: [swift-users] Compiled swift module to be used in swift REPL

2016-07-14 Thread Zhao Xin via swift-users
You can create a folder named Sources in your .playground file package and put your frameworks there. I guess you didn't use Xcode. Or you will find it yourself. [image: Inline image 1] Zhaoxin On Thu, Jul 14, 2016 at 3:36 PM, TUNG CK via swift-users < swift-users@swift.org> wrote: > I found ou

Re: [swift-users] Compiled swift module to be used in swift playground

2016-07-14 Thread Zhao Xin via swift-users
You can download this. https://static.realm.io/downloads/swift/realm-swift-1.0.2.zip You can find example applications for both iOS and OS X in our release zip > under examples/, demonstrating how to use many features of Realm like > migrations, how to use it with UITableViewControllers, encryptio

Re: [swift-users] didSet and time when propagation of mutation happens

2016-07-14 Thread Zhao Xin via swift-users
I think the problem is that you should not call callback?() in didSet() as the values in the closure are updated depending on the function, which means, it is not updated until the didSet() function finished. Zhaoxin On Fri, Jul 15, 2016 at 6:17 AM, Diego Sánchez wrote: > I don't know... when

Re: [swift-users] Why does Array subscript fail at runtime?

2016-07-20 Thread Zhao Xin via swift-users
For Array, it is easy to know its boundary. And it is an error when accessing indices out of the boundary. Returning a nil makes your app hard to trace bugs. Array's subscript function returns Element instead of Element?. It is common and aligns with the other languages. You question seems odd to

Re: [swift-users] try? works on non-method-call?

2016-07-21 Thread Zhao Xin via swift-users
I think the best way is do { let a = try couldFailButWillNot() if let b = a as? Int { print(b) } } catch let error { print(error) } It is longer, but much clearer. Below code will work, but not as clear. You don't know whether it is a or b causes the failure. It is

Re: [swift-users] @objc and private methods

2016-07-21 Thread Zhao Xin via swift-users
I think @objc makes the function an Objective-C function, so the private is no longer making the function private in Swift way, but in Objective-C way. In C++, sub-class can call super-class's private method. What you have to do is to override the function. class Test { @objc private func so

Re: [swift-users] @objc and private methods

2016-07-21 Thread Zhao Xin via swift-users
Then you should file a bug. Zhaoxin On Fri, Jul 22, 2016 at 1:03 AM, Tod Cunningham wrote: > If Test and Test2 are in separate files, swift doesn’t allow you to do the > override as it “can’t see” the definition of the private method in Test. > > > From: Zhao Xin > Date: Thursday, July 21, 201

Re: [swift-users] @objc and private methods

2016-07-21 Thread Zhao Xin via swift-users
. > Best, > Josh > > On Jul 21, 2016, at 10:48 AM, Zhao Xin via swift-users < > swift-users@swift.org<mailto:swift-users@swift.org>> wrote: > > Then you should file a bug. > > Zhaoxin > > On Fri, Jul 22, 2016 at 1:03 AM, Tod Cunningham < > tcunn

Re: [swift-users] Capturing structs by reference

2016-07-24 Thread Zhao Xin via swift-users
Below code works. struct MyValueType { var x: () -> () { return { print(self) } } init() { //self.x = { //print(self) //} } } let a = MyValueType() a.x() // MyValueType() Zhaoxin On Sun, Jul 24, 2016 at 5:11 PM, Tyler Fleming Cl

Re: [swift-users] Capturing structs by reference

2016-07-24 Thread Zhao Xin via swift-users
For the capture part, it is by strong reference as document says: “By default, a closure expression captures constants and variables from its > surrounding scope with strong references to those values. You can use a > capture list to explicitly control how values are captured in a closure.” > from

Re: [swift-users] Optional conformance warnings with Protocols

2016-07-24 Thread Zhao Xin via swift-users
I pasted you code into Xcode 8 beta3 playground, everything seemed fine with below code. import Cocoa import CoreLocation class ManifestItem: NSObject { let value:String let title:String let subtitle: String? dynamic var coordinate: CLLocationCoordinate2D init

Re: [swift-users] Optional conformance warnings with Protocols

2016-07-24 Thread Zhao Xin via swift-users
Implicitly unwrapped optionals and optionals are not equal. struct X { let value:String = "aa" } let x:X? = nil let y:X! = nil x?.value // nil y?.value // nil y.value // fatal error: unexpectedly found nil while unwrapping an Optional value. Unless you don't use this. As this is equal

Re: [swift-users] Optional conformance warnings with Protocols

2016-07-24 Thread Zhao Xin via swift-users
Yes. "Implicitly unwrapped optional are merely optionals". That is why Xcode gives you a warning instead of an error. In other situations, such as protocol claims an implicitly unwrapped optional but you give an optional, or protocol claims a non-optional but you give an implicitly unwrapped option

Re: [swift-users] UInt8 in the REPL

2016-07-25 Thread Zhao Xin via swift-users
You should file a bug on this, but it may be a known issue as Swift 3.0 together with Xcode 8 beta3 has no the same issue. Zhaoxin On Mon, Jul 25, 2016 at 5:46 PM, KS Sreeram via swift-users < swift-users@swift.org> wrote: > Hello > > In the Swift 2.2 REPL, UInt8(200) is displayed as -56. Is thi

Re: [swift-users] Compact iteration of optional collection?

2016-07-28 Thread Zhao Xin via swift-users
You can try container?.forEach(), like let bb:[String:Int]? = ["aa":1, "bb":2, "cc":3] bb?.forEach { print($0) } /* ("aa", 1) ("bb", 2) ("cc", 3) */ Zhaoxin On Fri, Jul 29, 2016 at 6:14 AM, Saagar Jha via swift-users < swift-users@swift.org> wrote: > The nil check and creating an empty a

Re: [swift-users] C Pointers and Memory

2016-07-29 Thread Zhao Xin via swift-users
Have you read https://developer.apple.com/library/tvos/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html#//apple_ref/doc/uid/TP40014216-CH8-ID17 ? Zhaoxin On Fri, Jul 29, 2016 at 4:55 PM, James Campbell via swift-users < swift-users@swift.org> wrote: > ​Do you know of a

Re: [swift-users] Learn Objective-C First? and Low Level Understanding of computer

2016-07-30 Thread Zhao Xin via swift-users
That depends on what you will do in the future. If you want to get a job, you should learn Objective-C as it is used widely in companies. If you just want to be an individual developer, you don't need to learn Objective-C. You can learn Swift directly. For low level programming part. You will have

Re: [swift-users] [Docs] - Documentation for the standard library

2016-08-01 Thread Zhao Xin via swift-users
https://developer.apple.com/reference/swift Xcode 8 beta 3 also has build-in help docs for swift 3. Zhaoxin On Mon, Aug 1, 2016 at 8:20 PM, Adrian Brink wrote: > Isn't the documentation described here >

[swift-users] What's the relationship between Swift Package Manager and Xcode?

2016-08-02 Thread Zhao Xin via swift-users
​Is Swift Package Manager, SPM, the competitor to Xcode packaging? Or is it the future of Xcode packing? If it is, when will it be put into Xcode? Also, what are the differences between built packages and Apple provided frameworks? Are they just the same thing with different names? Zhaoxin __

Re: [swift-users] What's the relationship between Swift Package Manager and Xcode?

2016-08-02 Thread Zhao Xin via swift-users
m fairly sure SPM currently >> only builds simple libraries). >> >> On the topic of what Xcode will become, this is not the place to discuss >> it. And I'm fairly sure you won't get any specifics from the Xcode team as >> it's still under Apple

Re: [swift-users] Cleaner way than if let initialization?

2016-08-04 Thread Zhao Xin via swift-users
It can't be done by ?? as stringFromDate(Date) returns String instead of String? Zhaoxin On Fri, Aug 5, 2016 at 1:42 AM, Jeff Kelley via swift-users < swift-users@swift.org> wrote: > That’s where I would use the ?? operator: > > let dobString = serverDateFormatter.stringFromDate(dob) ?? "" > >

Re: [swift-users] Cleaner way than if let initialization?

2016-08-04 Thread Zhao Xin via swift-users
Thank you. I think this is the best I saw today. Zhaoxin On Fri, Aug 5, 2016 at 5:24 AM, Erica Sadun via swift-users < swift-users@swift.org> wrote: > And Greg from Omni Group has an even better solution for you. Since > DateFormatter inherits from Formatter, you can use its `string(for:)` whic

Re: [swift-users] What are these types with regular expressions?

2016-08-06 Thread Zhao Xin via swift-users
TextCheckingResult i ​s​ NSTextCheckingResult 。 ​Zhaoxin​ On Sat, Aug 6, 2016 at 8:25 PM, 晓敏 褚 wrote: > I’m writing a program with regular expressions, and I’m finding it > extremely hard to code with these in swift. I g

Re: [swift-users] Help: I am unable to compile Swift with my own libraries.

2016-08-09 Thread Zhao Xin via swift-users
I think you should find out what are the differences between your tools and tools from apt-get. Maybe some configurations or versions do not feed the needs. That will be huge work as there are a lot of things may concern. I think you should follow the error as the lead first, which you didn't provi

[swift-users] How to get Swift 2.3 in command line?

2016-08-10 Thread Zhao Xin via swift-users
I try to change command line tool from Xcode 8.0(8S193k) to Xcode 7.3.1(7D1014), who lead me to swift 3.0 or swift 2.2. I need Swift 2.3 to build Realm Cocoa from its source. Any idea? Zhaoxin ___ swift-users mailing list swift-users@swift.org https://l

Re: [swift-users] How to get Swift 2.3 in command line?

2016-08-10 Thread Zhao Xin via swift-users
There are two toolchains under Xcode-beta.app/Contents/ Developer/Toolchains/. > Swift_2.3.xctoolchain/ > XcodeDefault.xctoolchain/ So in a terminal. input > export PATH=/Applications/Xcode-beta.app/Contents/Developer/ > Toolchains/Swift_2.3.xctoolchain/usr/bin:"${PATH}" After I did that, whe

Re: [swift-users] How to get Swift 2.3 in command line?

2016-08-11 Thread Zhao Xin via swift-users
but it seems as though this command works: > > ``` > xcrun --toolchain "com.apple.dt.toolchain.Swift_2_3" swiftc -v > ``` > > I got this identifier from: > > /Applications/Xcode-beta.app/Contents/Developer/Toolchains/ > Swift_2.3.xctoolchain/ToolchainInfo.plist > &

Re: [swift-users] How to get Swift 2.3 in command line?

2016-08-18 Thread Zhao Xin via swift-users
ain "com.apple.dt.toolchain.Swift_2_3" swiftc -v >> ``` >> >> I got this identifier from: >> >> /Applications/Xcode-beta.app/Contents/Developer/Toolchains/S >> wift_2.3.xctoolchain/ToolchainInfo.plist >> >> -- >> Keith Smiley >> >> On 08

[swift-users] Any reason of "@objc is not supported within extensions of generic classes." ?

2016-08-24 Thread Zhao Xin via swift-users
I was trying to implement a final class to conform NSCopying by extension. List was a class in Realm object database. final class List My implementation: extension List:NSCopying { public func copy(with zone: NSZone? = nil) -> Any { ... } } I got an error called "@objc is not su

Re: [swift-users] Any reason of "@objc is not supported within extensions of generic classes." ?

2016-08-24 Thread Zhao Xin via swift-users
ated dynamically at runtime. > > We could potentially support this some day, but it would take some > thought, and we'd have to be careful not to drastically slow down the cost > of instantiating new concrete types from a heavily-extended generic class. > > Jordan > > >

[swift-users] Can I do dynamic casting?

2016-08-25 Thread Zhao Xin via swift-users
Is that possible to do dynamic casting? My code: import Foundation let array = [1,2,3,4,5] let dictionary:[String:Any] = ["numbers":array] if let value = dictionary["numbers"] { let type = type(of: value) print(type) // Array let numbers = value as! Array // [1, 2, 3, 4, 5]

Re: [swift-users] Can I do dynamic casting?

2016-08-25 Thread Zhao Xin via swift-users
Thanks, Jordan. Is there any other way to do that? Zhaoxin On Thu, Aug 25, 2016 at 6:40 PM, Johan Segerfeldt wrote: > Zhaoxin, > > You are trying to cast to a type which is defined by a variable at runtime. > Casting with `as` is done at compile-time. The variable has no defined > value at this

Re: [swift-users] Shard Library using Swift

2016-08-26 Thread Zhao Xin via swift-users
It called modules in Swift. https://swift.org/package-manager/#conceptual-overview You can use `package manager` to creating a Library Package. Zhaoxin On Sat, Aug 27, 2016 at 1:33 AM, Reed Mangino via swift-users < swift-users@swift.org> wrote: > Is it possible to create a shared library in Sw

Re: [swift-users] protocols, optional, and public

2016-08-26 Thread Zhao Xin via swift-users
> > “Optional requirements are available so that you can write code that > interoperates with Objective-C. Both the protocol and the optional > requirement must be marked with the @objc attribute. ” > from: Apple Inc. “The Swift Programming Language (Swift 3 Beta)”。 iBooks. > https://itun.es/us/k

[swift-users] How self automatically becomes Self type if the function's return type is Self?

2016-08-27 Thread Zhao Xin via swift-users
See the code: protocol Foo { func instance() -> Self } class Bar: Foo { func instance() -> Self { return self // Declaration: let `self`: Self } func other() { let i = self // Declaration: let `self`: Bar } } How does it happen? Zhaoxin ___

Re: [swift-users] How self automatically becomes Self type if the function's return type is Self?

2016-08-28 Thread Zhao Xin via swift-users
t; String { >return “blah" > } > } > > -Kenny > > > > On Aug 27, 2016, at 7:05 AM, Zhao Xin via swift-users < > swift-users@swift.org> wrote: > > > > See the code: > > > > protocol Foo { > > func instance() -

[swift-users] Is 12:01:00A.M in DataFormatter.localizedString correct?

2016-08-28 Thread Zhao Xin via swift-users
It should be called 00:01:00 A.M., instead of 12:01:00 A.M. Shouldn't it? import Foundation let now = Date() var dc = Calendar.current.dateComponents([.year, .month, .day], from: now) dc.minute = 1 let date = Calendar.current.date(from: dc) print(DateFormatter.localizedString(from: date!, da

Re: [swift-users] Is 12:01:00A.M in DataFormatter.localizedString correct?

2016-08-28 Thread Zhao Xin via swift-users
Thank you, Jacob. It really was not what I expected. But I will remember it. Zhaoxin On Mon, Aug 29, 2016 at 12:16 PM, Jacob Bandes-Storch wrote: > 12:01 AM is the correct way of writing "one minute past midnight" in 12-hr > time. > > On Sun, Aug 28, 2016 at 9:15 PM, Zh

Re: [swift-users] Variadic parameter in function type

2016-08-29 Thread Zhao Xin via swift-users
How about this? func output(_ separator: String, _ terminator: String, _ items: Any...) { print(items, separator, terminator) } output(",", "\n", "Apple", "Banana") Zhaoxin On Mon, Aug 29, 2016 at 3:47 PM, Jin Wang wrote: > Hey Zhao, > > Thanks for your reply, but then i can’t use the

Re: [swift-users] Mapping a Dictionary to a Dictionary?

2016-08-29 Thread Zhao Xin via swift-users
I don''t quite understand your question. In Swift, Dictionaries are structs. You can always use `let dict2 = dict1`. That is enough. Or did you mean below code? var dict1 = ... var dict2 = ... for (key, value) in dic1 { dic2.updateValue(value, forKey:key) } Zhaoxin On Tue, Aug 30, 2016 at 4:

Re: [swift-users] Ambiguity passing `Any?` to generic constructor

2016-08-29 Thread Zhao Xin via swift-users
You can just remove public init(_ a: T) { actual = a } It is redundant. Below code works. public struct check { let actual: T? // //public init(_ a: T) { //actual = a //} public init(_ a: T?) { actual = a } public var isNil: Bo

Re: [swift-users] Mapping a Dictionary to a Dictionary?

2016-08-29 Thread Zhao Xin via swift-users
Jens, I see now. I believe this is talked months ago. There is no directly function at the moment. You can do: var dict1:[String:String] = ... var dict2: [User:Product] = [:] _ = dict1.map { dict2.updateValue(value, forKey:key) return nil } // use dict2 It is ugly, but it work

Re: [swift-users] Mapping a Dictionary to a Dictionary?

2016-08-29 Thread Zhao Xin via swift-users
I just thought updateValue(:,:) is more natural than subscript function in Dictionary. For example, if there is no key in the dictionary, both `dict2[key] = value` and `dict2.updateValue(value, forKey:key)` will add a new key-value pair. However, the subscript function does it by adding something

Re: [swift-users] Subtract a set of a subclass?

2016-08-31 Thread Zhao Xin via swift-users
I don't see the point. For example if an element in Set and another element in Set are with a same hash value. Neither of the elements should be subtracted. As they are in different types. And hash values between different types are not guaranteed to be comparable. import Foundation class Foo:Ha

Re: [swift-users] forEach error: Parameters may not have the 'var' specifier

2016-09-01 Thread Zhao Xin via swift-users
You issue is more like func foo(var bar:Int) { // error: Parameters may not have the 'var' specifier } So yes, `var` is not allow any more. Removing var from Function Parameters (SE-0003)

Re: [swift-users] Subtract a set of a subclass?

2016-09-01 Thread Zhao Xin via swift-users
Hi Nick, Glad to help. but when using third party classes I don’t know if the hash values are > comparable > You can create an extension with a convenient init(:), which creates a new instance of the super class basing on the instance of the sub class. That will guarantee the subtraction. Below

Re: [swift-users] Subtract a set of a subclass?

2016-09-01 Thread Zhao Xin via swift-users
differences between you and me. Zhaoxin On Fri, Sep 2, 2016 at 6:55 AM, Jordan Rose wrote: > > On Sep 1, 2016, at 15:44, Zhao Xin via swift-users > wrote: > > Hi Nick, > > Glad to help. > > but when using third party classes I don’t know if the hash values are >&

Re: [swift-users] Subtract a set of a subclass?

2016-09-01 Thread Zhao Xin via swift-users
aying a subclass should never override hashValue? Should Set not compare > elements with == instead of hashValue? > > Thanks > Nick > > M: +44 (0)7986 048 141 > W: http://nickbrook.me > > On 1 Sep 2016, at 23:55, Jordan Rose wrote: > > > On Sep 1, 2016, at 15:44,

Re: [swift-users] Subtract a set of a subclass?

2016-09-01 Thread Zhao Xin via swift-users
> What if a subclass does implement hashValue differently? It seems you are >> saying a subclass should never override hashValue? Should Set not compare >> elements with == instead of hashValue? >> >> Thanks >> Nick >> >> M: +44 (0)7986 048 141 >> W

[swift-users] Can we `override var hashValue`?

2016-09-02 Thread Zhao Xin via swift-users
In Objective-C, it says If two objects are equal, they must have the same hash value. This last > point is particularly important if you define isEqual: in a subclass and > intend to put instances of that subclass into a collection. Make sure you > also define hash in your subclass. https://devel

Re: [swift-users] Can we `override var hashValue`?

2016-09-02 Thread Zhao Xin via swift-users
7:02 AM, Dmitri Gribenko wrote: > On Sat, Sep 3, 2016 at 1:31 AM, Zhao Xin via swift-users > wrote: > > func ==(lhs: Apple, rhs: Apple) -> Bool { > > return lhs.name == rhs.name && lhs.shape == rhs.shape > > } > > > > func ==(lhs: Banana, rhs: B

Re: [swift-users] Can we `override var hashValue`?

2016-09-02 Thread Zhao Xin via swift-users
hashValue`* could guarantee the `hashValue` equality, isn't that enough? > > Zhaoxin > > > On Sat, Sep 3, 2016 at 7:02 AM, Dmitri Gribenko > wrote: > >> On Sat, Sep 3, 2016 at 1:31 AM, Zhao Xin via swift-users >> wrote: >> > func ==(lhs: Apple, rhs: A

Re: [swift-users] Subtract a set of a subclass?

2016-09-02 Thread Zhao Xin via swift-users
n, >>> >>> Thanks for the advice. >>> >>> What if a subclass does implement hashValue differently? It seems you >>> are saying a subclass should never override hashValue? Should Set not >>> compare elements with == instead of hashValue? >>> &

Re: [swift-users] Subtract a set of a subclass?

2016-09-02 Thread Zhao Xin via swift-users
to convert subclass to superclass as I did in previous replies. Zhaoxin On Sat, Sep 3, 2016 at 10:12 AM, Dmitri Gribenko wrote: > On Sat, Sep 3, 2016 at 4:47 AM, Zhao Xin via swift-users > wrote: > > Hi Jordan, > > > > Both you and I were wrong. > > > > My wrongne

Re: [swift-users] strange property observer behavior

2016-09-04 Thread Zhao Xin via swift-users
> > 1) when `didSet` observer will call? ​For me, it is more like Swift developer tries to override some beginner's flaw. ​2) infinite loop ​If you intended to do things bad, things ​went bad. 3) override property observer ​You mentioned "TSPL(The Swift Programming Language) ​", and it says

Re: [swift-users] strange property observer behavior

2016-09-04 Thread Zhao Xin via swift-users
> > 1) when `didSet` observer will call? ​For me, it is more like Swift developer tries to override some beginner's > flaw. Above is incorrect. You can change property's value in `didSet`, that won't cause didSet called again as it is intended to give you the opportunity to do that. ​2) infini

Re: [swift-users] strange property observer behavior

2016-09-04 Thread Zhao Xin via swift-users
It seems like a bug. You should file it. Zhaoxin On Sun, Sep 4, 2016 at 11:11 PM, wrote: > Thanks for reply. > > How does Swift choose *rules* as you said? > > Swfit encourage to override the property observer. But when we change the > own property in Child class's `didSet` observer, that would

Re: [swift-users] Can we `override var hashValue`?

2016-09-06 Thread Zhao Xin via swift-users
Thanks Lou. In addition to what Jordan and Dmitri have said, I think part of the > confusion is that you are assuming hash values are implicitly used in an > equality check. They are not. They are used when your instances are added > to certain types of collections. ​You are very nice. But I w

Re: [swift-users] Pass Value of Variable to a Function

2016-09-06 Thread Zhao Xin via swift-users
I think you messed up with `Locale` and `NSLocale`. `Locale` is a struct in Swift 3 to replace the legacy `NSLocale`. The latter is a class, it has an inner `structure` called `NSLocale.Key`. For `Locale`, there is no `NSLocale.Key`. All there keys are instance properties in `Locale`. So in your s

Re: [swift-users] Pass Value of Variable to a Function

2016-09-07 Thread Zhao Xin via swift-users
Now I understand your point. But as Jens said, Swift is a static language, it won't interpret `property` as a variable after `.`(dot). So for Swift compiler, you just refer to a none-exist property. Zhaoxin On Wed, Sep 7, 2016 at 12:39 PM, Michael Sheaver wrote: > Hi Zhao, > > Many thanks for y

[swift-users] 'Error' is ambiguous for type lookup in this context

2016-09-07 Thread Zhao Xin via swift-users
When I use RealmSwift in my project, I got an alert " 'Error' is ambiguous for type lookup in this context". import UIKit import UserNotifications import WatchConnectivity import RealmSwift extension AppDelegate:WCSessionDelegate { func session(_ session: WCSession, activationDidComplete

Re: [swift-users] 'Error' is ambiguous for type lookup in this context

2016-09-07 Thread Zhao Xin via swift-users
, 2016 at 10:52 AM, Shawn Erickson wrote: > For one RealmSwift should likely consider changing their enum cases to use > lowerCamelCase like Swift 3 did. Also at some point it should consider the > changes to NSError / Swift.Error bridging. > > -Shawn > > On Wed, Sep 7, 2016 at

Re: [swift-users] 'Error' is ambiguous for type lookup in this context

2016-09-07 Thread Zhao Xin via swift-users
>> use lowerCamelCase like Swift 3 did. Also at some point it should consider >>> the changes to NSError / Swift.Error bridging. >>> >>> -Shawn >>> >>> On Wed, Sep 7, 2016 at 7:18 PM Zhao Xin via swift-users < >>> swift-users@swift.or

Re: [swift-users] Migrating to Swift 3 using Xcode fails

2016-09-08 Thread Zhao Xin via swift-users
​I think you can just use Xcode's tool that you upgrade to Swift 3.0. However, I suggest you do a backup of your project first in case this is not helpful. Also, the errors you saw were probably not because of Xcode converted you code to 2.3 automatically. It wouldn't do that. The real reason is t

Re: [swift-users] Swift Playgrounds Not Included with iiOS 10.0.1 GM

2016-09-08 Thread Zhao Xin via swift-users
It is not on mine after upgrading to GM through OTA. Mine free space is 9.35GB. I think it can be easily explained. It is not a system app, it appeared in beta just because it is easily for us to test. It will appear in App Store when iOS 10 officially released. Zhaoxin On Fri, Sep 9, 2016 at 7:

Re: [swift-users] Migrating to Swift 3 using Xcode fails

2016-09-08 Thread Zhao Xin via swift-users
I ​ am confused with your situation. You said, "I’ve migrated one of my projects to Swift 3 previously, with an earlier beta of Xcode.". Then in Xcode should not using Swift 2.3. Assuming ​X ​code using 2.3 wrongly, you should use Xcode migration tool again, after that Xcode will use Swift 3.0. You

Re: [swift-users] Lazy expression is ambiguous?

2016-09-12 Thread Zhao Xin via swift-users
It is not a bug. You must explicitly specify the variable's type if you use `lazy` keyword. `lazy` means the variable will be initialized later, but the compiler needs its type right now. Zhaoxin On Mon, Sep 12, 2016 at 3:02 PM, Quinn "The Eskimo!" via swift-users < swift-users@swift.org> wrote:

Re: [swift-users] Stored Property on Protocol Extension

2016-09-12 Thread Zhao Xin via swift-users
​I don't know why the limit exists. Technical or on purpose. I hope someone inside can answer this. Zhaoxin​ On Mon, Sep 12, 2016 at 11:55 PM, Daniel Dunbar via swift-users < swift-users@swift.org> wrote: > > > On Sep 11, 2016, at 6:12 PM, Tanner Nelson via swift-users < > swift-users@swift.org>

Re: [swift-users] Method of same name is shadowing a property in Swift 3

2016-09-13 Thread Zhao Xin via swift-users
You said your issue only existed in Swift 3. However, you issue still exists in Xcode 7.3.1, which uses Swift 2.2. I tested your playground code. protocol QueryRow { var key: Any { get } func key(at index: UInt) -> Any? } var row: QueryRow row.key as? String Zhaoxin On Wed, Sep 14, 201

[swift-users] [swift-user]Unexpected behavior of protocol extension.

2016-09-19 Thread Zhao Xin via swift-users
See below code. protocol Foo { func bar() } extension Foo { func bar() { print("I am bar.") } } class A:Foo { func output() { print(type(of:self)) // prints "B". self.bar() // prints "I am bar." (self as! B).bar() // prints "I am B."

Re: [swift-users] reading keyboard input from xcode playgroud

2016-09-20 Thread Zhao Xin via swift-users
I test you code in command line `swiftc main.swift` and in macOS Command Line Tool app. Here is the main.swift: import Foundation import Foundation func input() -> String { let keyboard = FileHandle.standardInput let inputData = keyboard.availableData let strData = String(da

Re: [swift-users] Can anyone please explain this behavior?

2016-09-21 Thread Zhao Xin via swift-users
I suggest you defining the variables before using them like Marco does. Although from the global variable point of view, both way should be fine. But the glitch does exist, especially in playground. Zhaoxin On Thu, Sep 22, 2016 at 5:59 AM, Marco S Hyman via swift-users < swift-users@swift.org> wr

[swift-users] Why API marked as `macOS 10.12+` can be used in macOS 10.11?

2016-09-25 Thread Zhao Xin via swift-users
In the official migrating guide . You can choose from two kinds of migration to perform: - *Use Swift 2.3* Modifies your project to enable the *Use Legacy Swift* build setting and provides source changes to be able to build against the new SDKs. - *

Re: [swift-users] Why API marked as `macOS 10.12+` can be used in macOS 10.11?

2016-09-25 Thread Zhao Xin via swift-users
le attribute (check the generated > interface), any symbol defined in a Swift overlay is available for all > platform versions where swift 3 is compatible (iOS 7, macOS 10.9, etc). > > Jack > > On Sep 25, 2016, at 6:01 PM, Zhao Xin via swift-users < > swift-users@swift.org&g

Re: [swift-users] Any?

2016-09-27 Thread Zhao Xin via swift-users
`Any` is different from `Any?`. For example, if `Any` is an `Optional`, `Any?` is an `Optional>`. I ​n you code, since ​`dictionary` subscripts may `return nil`, the `?` is for that. The `Any` is for the type of the `none-nil return value`. Zhaoxin On Tue, Sep 27, 2016 at 6:43 PM, Седых Алекса

Re: [swift-users] Different behaviour when casting Float and Double to NSNumber

2016-10-05 Thread Zhao Xin via swift-users
I think you should file a bug on the inconsistence of `description`. However, the third-party API somehow using the `description` is not a good idea. Zhaoxin On Wed, Oct 5, 2016 at 5:30 PM, Lars-Jørgen Kristiansen via swift-users < swift-users@swift.org> wrote: > I'm working with a third party A

[swift-users] Is there an equivalence method on `NSArray`'s `func objects(at indexes: IndexSet) -> [Any]` in Array?

2016-10-06 Thread Zhao Xin via swift-users
I managed to do that by using `map` as I didn't find the equivalence. files.forEach { var indices = IndexSet() for index in 0 ..< subtitleFiles.count { if $0.sn == subtitleFiles[index].sn { indices.insert(index) } } let subtitles = indices.map

Re: [swift-users] Compiler refuses non-escaping closure calls in nested function

2016-10-09 Thread Zhao Xin via swift-users
I changed you code `let result = closure(n)` to `let result = closure(1)`. I thought that should eliminate the error. It didn't. The compiler asked me to change the whole code as below: func mainFunction(closure: @escaping (Int) -> Int) -> Int { func closureDoubled(_ n: Int) -> Int {

Re: [swift-users] Compiler refuses non-escaping closure calls in nested function: Bug?

2016-10-10 Thread Zhao Xin via swift-users
> It is just like there were two ways to do the job, you thought it worked in this way, but it chose the other way. Both ways lead to the same result. I think it is because the compiler **flatten** your inner function instead of calculated the result. The variable scope of `closure` is outside th

  1   2   >