Re: [swift-users] Array elements assign to variables

2017-01-27 Thread Ray Fix via swift-users
How about:

typealias PixelTuple = (red: UInt8, green: UInt8, blue: UInt8, brightness: 
UInt8)

let pixelCount = 10

let pointer = UnsafeMutableRawPointer.allocate(bytes: 4*pixelCount, alignedTo: 
16)
defer {
  pointer.deallocate(bytes: 4*pixelCount, alignedTo: 16)
}

let tuplesPointer = pointer.bindMemory(to: PixelTuple.self, capacity: 
pixelCount)
let tuples = UnsafeMutableBufferPointer(start: tuplesPointer, count: pixelCount)


for tuple in tuples {
  print(tuple.red, tuple.green, tuple.blue, tuple.brightness)
}




> On Jan 27, 2017, at 3:27 AM, TUNG CK via swift-users  
> wrote:
> 
> Python, we have this
>for pixel in pixels:
>r, g, b, brightness = pixel
>.
> 
> I wonder how to do it in swift? is there a more swifty way to do something 
> like this below?
>for pixel in pixels {
>var r = pixel[0]
>var g = pixel[1]
>var b = pixel[2]
>var brightness = pixel[3]
> .
> }
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Array elements assign to variables

2017-01-27 Thread TUNG CK via swift-users
Thanks 
I think we could only do this in swift

var (r,g,b,brightness) = (pixel[0],pixel[1],pixel[2],pixel[3])

> Alex Blewitt  
> 
>> On 27 Jan 2017, at 11:27 via swift-users  wrote:
>> 
>> Python, we have this
>>for pixel in pixels:
>>r, g, b, brightness = pixel
>>.
>> 
>> I wonder how to do it in swift? is there a more swifty way to do something 
>> like this below?
>>for pixel in pixels {
>>var r = pixel[0]
>>var g = pixel[1]
>>var b = pixel[2]
>>var brightness = pixel[3]
>> .
>> }
> 
> If Pixel is a tuple instead of an array, you can do:
> 
> var (r,g,b,brightness) = pixel
> 
> Alex
> 
>   1> let Pixel = (1,2,3,4)
> Pixel: (Int, Int, Int, Int) = {
>   0 = 1
>   1 = 2
>   2 = 3
>   3 = 4
> }
>   2> var (r,g,b,brightness) = Pixel
> r: Int = 1
> g: Int = 2
> b: Int = 3
> brightness: Int = 4
>   3>  
> 
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] How do I word wrap a UIButton title in code?

2017-01-27 Thread Adrian Zubarev via swift-users
If I remember correctly the property on the UIButton is called 
`.lineBreakMode`, which accepts an enum.  

--  
Adrian Zubarev
Sent with Airmail  

Am 28. Januar 2017 um 00:46:18, Mutlu Qwerty via swift-users 
(swift-users@swift.org(mailto:swift-users@swift.org)) schrieb:

>  
>  
> How do I make a UIButton wrap lines in the Title? Here is a sample code:  
>  
>  
>  
>  
>  
>  
> func testing() {
>  
>  
> let aButton = UIButton(type: .system)
>  
>  
> aButton.lineWrap = true //How do I make aButton wrap lines?
>  
>  
> aButton.setTitle("123\n456\n789", for: .normal)
>  
>  
> self.view.addSubview(aButton)
>  
>  
> }
>  
>  
>  
>  
>  
>  
> What I want to see is a button like this:
>  
>  
> ———
>  
>  
> |123|
>  
>  
> |456|
>  
>  
> |789|
>  
>  
> ——-
>  
>  
>  
>  
>  
>  
> This can be done in the Storyboard by selecting “Word Wrap” in the Button’s 
> Control - Line Break attribute. But I am creating my UIButtons in code and 
> can not find the proper syntax to code this.
>  
>  
>  
>  
>  
>  
>  
>  
>  
> Thomas Cavalli
>  
>  
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] How do I word wrap a UIButton title in code?

2017-01-27 Thread Mutlu Qwerty via swift-users
How do I make a UIButton wrap lines in the Title?  Here is a sample code:


   func testing() {

let aButton = UIButton(type: .system)

aButton.lineWrap = true  //How do I make aButton wrap lines?

aButton.setTitle("123\n456\n789", for: .normal)

self.view.addSubview(aButton)

}


What I want to see is a button like this:

 ---

|123|

|456|

|789|

 ---


This can be done in the Storyboard by selecting "Word Wrap" in the Button's 
Control - Line Break attribute.  But I am creating my UIButtons in code and can 
not find the proper syntax to code this.


Thomas Cavalli
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Swift indentation for vim

2017-01-27 Thread Keith Smiley via swift-users
If you're interested in contributing that script to my repo I'm
definitely open to it!

There are a ton of issues, as you saw with the current script that I
just haven't spent the time to try and fix lately, so it would be great
to start from a new robust base.

--
Keith Smiley

On 01/27, Aaron Bohannon via swift-users wrote:
> You have probably noticed that the available vim plugins do not provide
> robust support for automatic indentation of Swift code.  The most complete
> vim plugin appears to be the one here:
>
> https://github.com/keith/swift.vim
>
> I found that its indentation script would frequently get confused, though.
> Therefore, it seemed necessary for me to create a new indentation script.
> Mine is configurable and (appears to be) extremely robust.  My script is
> available here:
>
> https://gist.github.com/morefun2compute/bdc6ee6d8b9add60a03b318bd91ba570
>
> Unfortunately, I cannot distribute my script as a standalone plugin because
> it depends upon syntax highlighting support, which I have not
> re-implemented (nor do I plan to re-implement it at this point in time).
> If any of you have advice on how to distribute my script so that it can be
> easily installed, please let me know.  For now, you will have to copy my
> file into the location `.../indent/swift.vim` within the plugin I mentioned
> above.  (I suspect it will also work with the vim plugin supplied in the
> Swift source tree, although you will have to add the `indent` directory
> manually.)
>
> - Aaron

> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] Swift indentation for vim

2017-01-27 Thread Aaron Bohannon via swift-users
You have probably noticed that the available vim plugins do not provide
robust support for automatic indentation of Swift code.  The most complete
vim plugin appears to be the one here:

https://github.com/keith/swift.vim

I found that its indentation script would frequently get confused, though.
Therefore, it seemed necessary for me to create a new indentation script.
Mine is configurable and (appears to be) extremely robust.  My script is
available here:

https://gist.github.com/morefun2compute/bdc6ee6d8b9add60a03b318bd91ba570

Unfortunately, I cannot distribute my script as a standalone plugin because
it depends upon syntax highlighting support, which I have not
re-implemented (nor do I plan to re-implement it at this point in time).
If any of you have advice on how to distribute my script so that it can be
easily installed, please let me know.  For now, you will have to copy my
file into the location `.../indent/swift.vim` within the plugin I mentioned
above.  (I suspect it will also work with the vim plugin supplied in the
Swift source tree, although you will have to add the `indent` directory
manually.)

- Aaron
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Initializers

2017-01-27 Thread Jordan Rose via swift-users

> On Jan 27, 2017, at 11:04, Slava Pestov via swift-users 
>  wrote:
> 
>> 
>> On Jan 27, 2017, at 10:39 AM, tuuranton--- via swift-users 
>> mailto:swift-users@swift.org>> wrote:
>> 
>> Yes, but why?
>> 
>> What's the rationale for this?
>> 
>> What would be so bad about allowing overriding a non-failable initializer 
>> with a failable initializer?
> 
> If the non-failable initializer witnesses a non-failable protocol 
> requirement, and the subclass overrides it, what should be the runtime 
> behavior if the subclass initializer returns nil? The caller won’t expect it, 
> since the caller is calling a non-failable initializer.

This particular objection only applies to 'required' initializers, though.


> 
> For similar reasons, you cannot override a method that returns a non-optional 
> value with a method returning an optional value.

Yes, this is true.

Jordan

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] EnumerateUsingObjects in Swift

2017-01-27 Thread Doug Hill via swift-users
Ah, thanks for the info about Swift syntax. I guess my C++ background trips me 
up on some of this Swift syntax.

Doug Hill

> On Jan 25, 2017, at 11:32 PM, Pierre Monod-Broca  
> wrote:
> 
> You should try:
> ```
> class Test {
> let array: [T] = []
> . . .
> ```
> 
> Because when you declare `class Test`, NSObject is the generic 
> parameter, no longer the class.
> 
> Pierre
> 
> Le 26 janv. 2017 à 02:45, Doug Hill via swift-users  > a écrit :
> 
>> Unfortunately, this is for a work-related project, not just research, so I'm 
>> looking for solutions to specific problems that I'll need for my work.
>> 
>> I guess I'll have to file this one away as broken in Swift 2.x
>> 
>> Doug
>> 
>>> On Jan 25, 2017, at 5:28 PM, Zhao Xin >> > wrote:
>>> 
>>> I think in swift 2.x, the `Array` is not mature enough to do a lot of 
>>> things. Besides, `NSArray` can also hold non-NSObjects like Int, for 
>>> example. 
>>> 
>>> I suggest you to use the latest Swift to do your research. As in Swift, 
>>> everything moves fast and changes a lot. In the latest Xcode beta, all 
>>> swift 2.x are abandoned. The latest stable swift is 3.0.2 and 2.3. The next 
>>> release will be swift 3.1.
>>> 
>>> Zhaoxin
>>> 
>>> On Thu, Jan 26, 2017 at 9:17 AM, Doug Hill >> > wrote:
>>> I'm guessing that conversion of a Swift array to an NSArray can only happen 
>>> if the Swift array holds NSObjects. So, I tried changing the type parameter 
>>> of my class to NSObject:
>>> 
>>> class Test
>>> {
>>> let array = [NSObject]()
>>> 
>>> init() {
>>> let temp = self.array as NSArray
>>> }
>>> }
>>> 
>>>  error: cannot convert value of type '[NSObject]' to type 'NSArray' in 
>>> coercion
>>> var temp  = self.array  as NSArray
>>> ~^
>>> 
>>> However, if I change the type parameter to something else it compiles with 
>>> no problem.
>>> 
>>> class Test
>>> {
>>> let array = [NSObject]()
>>> 
>>> init() {
>>> let temp = self.array as NSArray
>>> }
>>> }
>>> 
>>> 
>>> I guess this is interesting, but I still can't create an array with items 
>>> whose type is the type parameter of the class and then convert to NSArray.
>>> 
>>> Doug Hill
>>> 
>>> 
 On Jan 25, 2017, at 10:49 AM, Doug Hill >>> > wrote:
 
 OK, I just tried testing this code in my app and a Swift playground. I 
 also tried a variation on the initializer just for the heck of it. I get 
 the following error:
 
 class Test
 {
 var array:[T] = []
 var array2 = [T]()
 
 init() {
 var temp  = self.array as NSArray
 var temp2 = self.array2 as NSArray
 }
 }
 
 error: cannot convert value of type '[T]' to type 'NSArray' in coercion
 var temp = self.array as NSArray
~^
 error: cannot convert value of type '[T]' to type 'NSArray' in coercion
 var temp2 = self.array2 as NSArray
 ~^
 
 Are there restrictions on what can be converted to NSArray?
 
 Doug Hill
 
> On Jan 25, 2017, at 9:24 AM, Doug Hill via swift-users 
> mailto:swift-users@swift.org>> wrote:
> 
> Thanks for the help. I'm still trying to figure out how Swift works, 
> particularly what the error messages mean. This has been driving me a 
> little nuts trying to figure out what is wrong via sometimes cryptic 
> errors. Also, it seems like getting generic programming working in Swift 
> is more difficult than I'm used to (even than C++!) so this answer helps 
> figure out how the compiler works.
> 
> Doug Hill
> 
> 
>> On Jan 23, 2017, at 7:04 PM, Zhao Xin > > wrote:
>> 
>> It seems to me that you didn't initialize your `myArray` before you 
>> casted it. That caused the problem.
>> 
>> Zhaoxin
>> 
>> On Tue, Jan 24, 2017 at 9:34 AM, Jon Shier via swift-users 
>> mailto:swift-users@swift.org>> wrote:
>> enumerateObjects(options:using:) exists on NSArray in Swift. And I was 
>> able to create your generic class just fine:
>> 
>> class Test {
>> var array: [T] = []
>> 
>> init() {
>> var temp = array as NSArray
>> }
>> }
>>> 
>> 
>> ___
>> swift-users mailing list
>> swift-users@swift.org 
>> https://lists.swift.org/mailman/listinfo/swift-users 
>> 

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Initializers

2017-01-27 Thread Slava Pestov via swift-users

> On Jan 27, 2017, at 10:39 AM, tuuranton--- via swift-users 
>  wrote:
> 
> Yes, but why?
> 
> What's the rationale for this?
> 
> What would be so bad about allowing overriding a non-failable initializer 
> with a failable initializer?

If the non-failable initializer witnesses a non-failable protocol requirement, 
and the subclass overrides it, what should be the runtime behavior if the 
subclass initializer returns nil? The caller won’t expect it, since the caller 
is calling a non-failable initializer.

For similar reasons, you cannot override a method that returns a non-optional 
value with a method returning an optional value.

Slava

> 
> 27. Jan 2017 18:59 by saa...@saagarjha.com :
> 
> You can’t override a designated initializer with one that is failable. The 
> second one is defining a new initializer that is failable, instead of 
> overriding the one from its superclass.
> 
> Saagar Jha
> 
> On Jan 27, 2017, at 8:45 AM, tuuranton--- via swift-users 
> mailto:swift-users@swift.org>> wrote:
> 
> See the comments. Why is one allowed but the other one isn't and what's the 
> rationale for this?
> 
> 
> class Vehicle {
> let name: String
> init(name: String) {
> self.name = name
> }
> }
> 
> 
> class Car: Vehicle {
> //Why is this not allowed?
> override init?(name: String) {
> super.init(name: name)
> }
> 
> //But this is allowed?
> init?(name: String, ignore: String) {
> super.init(name: name)
> }
> }
> 
> 
> 
> ___
> swift-users mailing list
> swift-users@swift.org 
> https://lists.swift.org/mailman/listinfo/swift-users 
> 
> 
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Initializers

2017-01-27 Thread Jordan Rose via swift-users
Convenience initializers are inherited when you override all of your parent 
class's designated initializers. If you change the signature of the designated 
initializer, then the convenience initializers will no longer do the right 
thing.

However, we could allow this case and just consider it shadowing the original 
initializer rather than overriding. In that case, though, we'd probably want 
something like @nonoverriding to make it clear that it was intentional.

Jordan


> On Jan 27, 2017, at 10:39, tuuranton--- via swift-users 
>  wrote:
> 
> Yes, but why?
> 
> What's the rationale for this?
> 
> What would be so bad about allowing overriding a non-failable initializer 
> with a failable initializer?
> 
> 27. Jan 2017 18:59 by saa...@saagarjha.com :
> 
> You can’t override a designated initializer with one that is failable. The 
> second one is defining a new initializer that is failable, instead of 
> overriding the one from its superclass.
> 
> Saagar Jha
> 
> On Jan 27, 2017, at 8:45 AM, tuuranton--- via swift-users 
> mailto:swift-users@swift.org>> wrote:
> 
> See the comments. Why is one allowed but the other one isn't and what's the 
> rationale for this?
> 
> 
> class Vehicle {
> let name: String
> init(name: String) {
> self.name = name
> }
> }
> 
> 
> class Car: Vehicle {
> //Why is this not allowed?
> override init?(name: String) {
> super.init(name: name)
> }
> 
> //But this is allowed?
> init?(name: String, ignore: String) {
> super.init(name: name)
> }
> }
> 
> 
> 
> ___
> swift-users mailing list
> swift-users@swift.org 
> https://lists.swift.org/mailman/listinfo/swift-users 
> 
> 
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] @NSCopying semantic does not appear to copy in Swift initializer

2017-01-27 Thread Jordan Rose via swift-users
Your observation is correct: @NSCopying currently does not affect initializers. 
This is because accessing a property in an initializer always does direct 
access to the storage rather than going through the setter. It might be 
reasonable to change this behavior, but it probably deserves a bit of 
discussion on swift-evolution; it's not 100%, for-sure a bug. (There is a Radar 
for this, rdar://problem/21383959 , but no 
bugs.swift.org  issue.)

Jordan

> On Jan 26, 2017, at 23:30, Torin Kwok via swift-users  
> wrote:
> 
> Hello guys,
> 
> I wanna ask a question about the behavior of `@NSCopying` semantic in
> Swift 3. Well, according to Apple's official documentation:
> 
>> In Swift, the Objective-C copy property attribute translates to
>> @NSCopying. The type of the property must conform to the NSCopying
>> protocol.
> 
> However, I encountered a strange behavior when I declared a property
> with the `@NSCopying` attribute:
> 
> ```
> // `Person` class inherits from `NSObject` class and conforms to `NSCopying` 
> protocol
> @NSCopying var employee: Person
> ```
> 
> and then assigned an external instance of `Person` class protocol to
> this property within the designated init methods:
> 
> ```
> // Designated initializer of `Department` class
> init( employee externalEmployee: Person ) {
>  self.employee = externalEmployee
>  super.init()
> 
>  // Assertion would fail because Swift do not actually copy the value 
> assigned to this property 
>  // even though `self.employee` has been marked as `@NSCoyping`
>  // assert( self.employee !== externalEmployee )
>  }
> ```
> 
> If I indeed require the deep copying behavior during the init process,
> instead of making advantage of `@NSCopying` attribute, I have to
> invoke the `copy()` method manually:
> 
> ```
> init( employee externalEmployee: Person ) {
>  // ...
>  self.employee = externalEmployee.copy() as! Person  
>  // ...
>  }
> ```
> 
> In fact, what really makes me confusing is that `@NSCopying` semantic
> does work properly within the other parts of the class definition such
> as normal instance methods, or external scope. For instance, if we're
> assigning an external instance of `Person` to the `self.employee` proper
> of `Department` directly through setter rather than initializer:
> 
>> department.employee = johnAppleseed
> 
> then `self.employee` property and `johnAppleseed` variable will no
> longer share the same underlying object now. In the other words,
> `@NSCopying` attribute makes sense.
> 
> After I looked through a great deal of results given by Google, and
> dicussions on StackOverflow, I finally found nothing related — the vast
> majority of articles, documentations as well as issues talking about
> this similar topics only focus on the basic concepts and effects of
> `@NSCopying` itself but do not mentioned this strange behavior at all —
> besides one radar descriping the same problem (rdar://21383959) and a
> final conclusion mentioned in a guy's Gist comment: **... values set
> during initialization are not cloned ...**
> 
> That is, `@NSCopying` semantic has no effect in initializers.
> 
> Then, what I want to figure out is the reason why `@NSCopying` semantic
> will become effectless implicitly whithin initializers of a class, and
> the special considerations behind this behavior, if any.
> 
> Thank you very much.
> 
> Best Regards,
> Torin Kwok
> 
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Initializers

2017-01-27 Thread tuuranton--- via swift-users
Yes, but why?
What's the rationale for this?
What would be so bad about allowing overriding a non-failable initializer with 
a failable initializer?
27. Jan 2017 18:59 by saa...@saagarjha.com:


> You can’t override a designated initializer with one that is failable. The 
> second one is defining a new initializer that is failable, instead of 
> overriding the one from its superclass.
> Saagar Jha
>
>> On Jan 27, 2017, at 8:45 AM, tuuranton--- via swift-users <>> 
>> swift-users@swift.org>> > wrote:
>>   >> See the comments. Why is one allowed but the other one isn't 
>> and what's the rationale for this?
>>
>> class Vehicle {>>     let name: String>>     init(name: String) {>>         
>> self.name = name>>     }>> }
>>
>> class Car: Vehicle {>>     //Why is this not allowed?>>     override 
>> init?(name: String) {>>         super.init(name: name)>>     }>>     >>     
>> //But this is allowed?>>     init?(name: String, ignore: String) {>>         
>> super.init(name: name)>>     }>> }
>>
>>
>>   >> ___
>> swift-users mailing list
>> swift-users@swift.org
>> https://lists.swift.org/mailman/listinfo/swift-users
>>
>
>___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Initializers

2017-01-27 Thread Saagar Jha via swift-users
You can’t override a designated initializer with one that is failable. The 
second one is defining a new initializer that is failable, instead of 
overriding the one from its superclass.

Saagar Jha

> On Jan 27, 2017, at 8:45 AM, tuuranton--- via swift-users 
>  wrote:
> 
> See the comments. Why is one allowed but the other one isn't and what's the 
> rationale for this?
> 
> 
> class Vehicle {
> let name: String
> init(name: String) {
> self.name = name
> }
> }
> 
> 
> class Car: Vehicle {
> //Why is this not allowed?
> override init?(name: String) {
> super.init(name: name)
> }
> 
> //But this is allowed?
> init?(name: String, ignore: String) {
> super.init(name: name)
> }
> }
> 
> 
> 
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] Initializers

2017-01-27 Thread tuuranton--- via swift-users
See the comments. Why is one allowed but the other one isn't and what's the 
rationale for this?

class Vehicle {    let name: String    init(name: String) {        self.name = 
name    }}

class Car: Vehicle {    //Why is this not allowed?    override init?(name: 
String) {        super.init(name: name)    }        //But this is allowed?    
init?(name: String, ignore: String) {        super.init(name: name)    }}


___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] SPM: Library dependency bug?

2017-01-27 Thread Rien via swift-users
I have a system wrapper module/package for openSSL called COpenSsl.

I have another package (called SecureSockets) that uses COpenSsl. This works 
fine.

To create a framework, I then added a xcode project to SecureSockets and build 
the framework. This also works fine.

Then I created an App (macOS) and imported the SecureSockets.framework. 
(Included the headers in a bridge file, updated the search paths)
However I cannot use the SecureSockets module, whenever I try that I get an 
error message saying that COpenSsl is missing.
The App does not need COpenSsl, other than through the wrapper/framework in 
SecureSockets.

To work around this I created an empty library project called COpenSsl and 
included that framework in the App as well.

This satisfies the compiler and I now have a working App.

Is it a bug that COpenSsl is needed in the App? or am I doing something wrong?

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl





___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] 3.0.3?

2017-01-27 Thread Swizzlr via swift-users
Is there any appetite among the core team in backporting bug fixes from 
Corelibs into 3.0? 3.1 isn't yet stable enough for production but I would quite 
like functional JSON generation.

Tom

Sent from my iPhone
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] @NSCopying semantic does not appear to copy in Swift initializer

2017-01-27 Thread Rod Brown via swift-users
Hi Torin,

Just a few notes:

I believe the @NSCopying behaviour actually uses the copy(with zone: NSZone?) 
method rather than the indirect copy() method. I would check that you’ve 
implemented that correctly. If you have, then it appears to be a bug. I would 
breakpoint on this method (not copy()) to check the copy is actually occurring.

I would note however that this check doesn’t necessarily work for some of 
Apple’s API’s though. A lot of the Foundation API’s implement copy avoidance. 
That is, if you copy() an NSArray, it will actually just return itself, 
retained, rather than a new array, because an NSArray is immutable, and so the 
copy is needless. NSMutableArray then overrides the method and actually does 
return a true copy, because it is mutable and therefore dangerous. A lot of the 
foundation classes that implement NSCopying do this (think of this as a poor 
man’s copy-on-write!), and so the !== check isn’t necessarily the correct check 
to ensure it is setting via NSCopying. Breakpointing or checking with mutable 
variant classes is a more correct check with Foundation-style APIs.

Thanks,

Rod

> On 27 Jan 2017, at 6:30 pm, Torin Kwok via swift-users 
>  wrote:
> 
> Hello guys,
> 
> I wanna ask a question about the behavior of `@NSCopying` semantic in
> Swift 3. Well, according to Apple's official documentation:
> 
>> In Swift, the Objective-C copy property attribute translates to
>> @NSCopying. The type of the property must conform to the NSCopying
>> protocol.
> 
> However, I encountered a strange behavior when I declared a property
> with the `@NSCopying` attribute:
> 
> ```
> // `Person` class inherits from `NSObject` class and conforms to `NSCopying` 
> protocol
> @NSCopying var employee: Person
> ```
> 
> and then assigned an external instance of `Person` class protocol to
> this property within the designated init methods:
> 
> ```
> // Designated initializer of `Department` class
> init( employee externalEmployee: Person ) {
>  self.employee = externalEmployee
>  super.init()
> 
>  // Assertion would fail because Swift do not actually copy the value 
> assigned to this property 
>  // even though `self.employee` has been marked as `@NSCoyping`
>  // assert( self.employee !== externalEmployee )
>  }
> ```
> 
> If I indeed require the deep copying behavior during the init process,
> instead of making advantage of `@NSCopying` attribute, I have to
> invoke the `copy()` method manually:
> 
> ```
> init( employee externalEmployee: Person ) {
>  // ...
>  self.employee = externalEmployee.copy() as! Person  
>  // ...
>  }
> ```
> 
> In fact, what really makes me confusing is that `@NSCopying` semantic
> does work properly within the other parts of the class definition such
> as normal instance methods, or external scope. For instance, if we're
> assigning an external instance of `Person` to the `self.employee` proper
> of `Department` directly through setter rather than initializer:
> 
>> department.employee = johnAppleseed
> 
> then `self.employee` property and `johnAppleseed` variable will no
> longer share the same underlying object now. In the other words,
> `@NSCopying` attribute makes sense.
> 
> After I looked through a great deal of results given by Google, and
> dicussions on StackOverflow, I finally found nothing related — the vast
> majority of articles, documentations as well as issues talking about
> this similar topics only focus on the basic concepts and effects of
> `@NSCopying` itself but do not mentioned this strange behavior at all —
> besides one radar descriping the same problem (rdar://21383959) and a
> final conclusion mentioned in a guy's Gist comment: **... values set
> during initialization are not cloned ...**
> 
> That is, `@NSCopying` semantic has no effect in initializers.
> 
> Then, what I want to figure out is the reason why `@NSCopying` semantic
> will become effectless implicitly whithin initializers of a class, and
> the special considerations behind this behavior, if any.
> 
> Thank you very much.
> 
> Best Regards,
> Torin Kwok
> 
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Array elements assign to variables

2017-01-27 Thread Alex Blewitt via swift-users
On 27 Jan 2017, at 11:27, TUNG CK via swift-users  wrote:
> 
> Python, we have this
>for pixel in pixels:
>r, g, b, brightness = pixel
>.
> 
> I wonder how to do it in swift? is there a more swifty way to do something 
> like this below?
>for pixel in pixels {
>var r = pixel[0]
>var g = pixel[1]
>var b = pixel[2]
>var brightness = pixel[3]
> .
> }

If Pixel is a tuple instead of an array, you can do:

var (r,g,b,brightness) = pixel

Alex

  1> let Pixel = (1,2,3,4)
Pixel: (Int, Int, Int, Int) = {
  0 = 1
  1 = 2
  2 = 3
  3 = 4
}
  2> var (r,g,b,brightness) = Pixel
r: Int = 1
g: Int = 2
b: Int = 3
brightness: Int = 4
  3>  

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] Array elements assign to variables

2017-01-27 Thread TUNG CK via swift-users
Python, we have this
for pixel in pixels:
r, g, b, brightness = pixel
.

I wonder how to do it in swift? is there a more swifty way to do something like 
this below?
for pixel in pixels {
var r = pixel[0]
var g = pixel[1]
var b = pixel[2]
var brightness = pixel[3]
 .
 }
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users