Re: [swift-users] EnumerateUsingObjects in Swift

2017-01-25 Thread Pierre Monod-Broca via swift-users
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 
>  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 
>>>  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] [swift-evolution] Best way to handle escaping function that might throw

2017-01-12 Thread Pierre Monod-Broca via swift-users
@Howard
I was thinking the same about the constraint.

Does that mean that Never should be a subtype of Error (maybe it is already) ? 
Else you couldn't say throws(Never) or FSTore

@Howard
I'm not sure about <>, it should stay reserved for generics, IMHO.

Pierre

> Le 13 janv. 2017 à 01:11, Howard Lovatt via swift-users 
>  a écrit :
> 
> @Anton, Yes that would work and we would get typed throws, which I like. As 
> an aside, I think E would have to be constrained to be an Error, ` Error>`, and maybe `throws` reads better because you are used to types in 
> angle brackets.
> 
>   -- Howard.
> 
>> On 13 January 2017 at 08:32, Anton Zhilin  wrote:
>> The best way to deal with such situations should be typed throws. Then 
>> rethrows should be removed and replaced with generics in throws clause. E == 
>> Never ⇔ function does not throw.
>> 
>> struct FStore {
>> let f: () throws(E) -> Void
>> init(_ f: @escaping () throws(E) -> Void) { self.f = f }
>> func call() throws(E) { try f() }
>> }
>> 
>> let store = FStore({ print("Hello") })
>> store.call()
>> [Phase2]
>> 
> 
> ___
> 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] make a static/class method return type be the subclass it is called with

2017-01-10 Thread Pierre Monod-Broca via swift-users
Hi again,

You might want to look at Self requirement in protocols for exemple:
protocol P {
  func items(/*...*/) -> [Self]
}
class C: P {
  func items(/*...*/) -> [C] {
...
  }
}

However it might not always work as you expect.

I can't say which is the better. Using associated type might be more flexible. 

Pierre

> Le 5 janv. 2017 à 21:10, davel...@mac.com a écrit :
> 
> Yes, it does seem to work. I just wondered if there was a better way to do 
> it. Perhaps by writing the method in a base class (but I couldn't get that to 
> work) instead of using an extension. Then I could just inherit from that base 
> class rather than needing to write:
> 
> extension Event: DDRCoreData {
>  typealias Entity = Event
> }
> 
> in addition to class Event: NSManagedObject . . .
> 
> Yes, I know in Xcode 8, that last part can now be written for you.
> 
> Again, if this is the "right way to do it", I'm ok with that, I just wondered 
> if there is a better way to do it.
> 
> Thanks,
> Dave Reed
> 
> 
>> On Jan 5, 2017, at 2:35 PM, Pierre Monod-Broca  
>> wrote:
>> 
>> Hello,
>> 
>> It looks that you have what you wanted because Event.Entity is an alias of 
>> Event. 
>> 
>> Pierre
>> 
>>> Le 5 janv. 2017 à 16:47, Dave Reed via swift-users  
>>> a écrit :
>>> 
>>> Is there a way to make a static or class method specify the return type be 
>>> the actual class it is called with?
>>> 
>>> The example code below using protocols/extensions mostly works (the type is 
>>> [Event.Entity] not [Event] but it seems to work.
>>> 
>>> If I try to make DDRCoreData a base class and Event subclass it, I'm only 
>>> able to get the return type to be [DDRCoreData], not [Event] when I call it 
>>> with Event.items(in: moc)
>>> 
>>> Here is the code that mostly works using protocols. Is there a better way 
>>> to do this?
>>> 
>>> protocol DDRCoreData {
>>>  associatedtype Entity: NSManagedObject
>>> 
>>>  static func items(in context: NSManagedObjectContext, matching predicate: 
>>> NSPredicate?, sortedBy sorters: [NSSortDescriptor]?) -> [Entity]
>>> }
>>> 
>>> extension DDRCoreData {
>>>  static func items(in context: NSManagedObjectContext, matching predicate: 
>>> NSPredicate? = nil, sortedBy sorters: [NSSortDescriptor]? = nil) -> 
>>> [Entity] {
>>>  var items: [Entity] = []
>>>  context.performAndWait {
>>>  let fetchRequest: NSFetchRequest = Entity.fetchRequest() 
>>> as! NSFetchRequest
>>>  fetchRequest.predicate = predicate
>>>  fetchRequest.sortDescriptors = sorters
>>>  do {
>>>  items = try fetchRequest.execute() as [Entity]
>>>  } catch {
>>> 
>>>  }
>>>  }
>>>  return items
>>>  }
>>> }
>>> 
>>> @objc(Event)
>>> public class Event: NSManagedObject {
>>> 
>>> }
>>> 
>>> extension Event: DDRCoreData {
>>>  typealias Entity = Event
>>> }
>>> 
>>> // compiler says items is of type [Event.Entity]
>>> let items = Event.items(in: controller.managedObjectContext!)
>>> 
>>> Thanks,
>>> Dave Reed
>>> 
>>> 
>>> ___
>>> 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] make a static/class method return type be the subclass it is called with

2017-01-05 Thread Pierre Monod-Broca via swift-users
Hello,

It looks that you have what you wanted because Event.Entity is an alias of 
Event. 

Pierre

> Le 5 janv. 2017 à 16:47, Dave Reed via swift-users  a 
> écrit :
> 
> Is there a way to make a static or class method specify the return type be 
> the actual class it is called with?
> 
> The example code below using protocols/extensions mostly works (the type is 
> [Event.Entity] not [Event] but it seems to work.
> 
> If I try to make DDRCoreData a base class and Event subclass it, I'm only 
> able to get the return type to be [DDRCoreData], not [Event] when I call it 
> with Event.items(in: moc)
> 
> Here is the code that mostly works using protocols. Is there a better way to 
> do this?
> 
> protocol DDRCoreData {
>associatedtype Entity: NSManagedObject
> 
>static func items(in context: NSManagedObjectContext, matching predicate: 
> NSPredicate?, sortedBy sorters: [NSSortDescriptor]?) -> [Entity]
> }
> 
> extension DDRCoreData {
>static func items(in context: NSManagedObjectContext, matching predicate: 
> NSPredicate? = nil, sortedBy sorters: [NSSortDescriptor]? = nil) -> [Entity] {
>var items: [Entity] = []
>context.performAndWait {
>let fetchRequest: NSFetchRequest = Entity.fetchRequest() 
> as! NSFetchRequest
>fetchRequest.predicate = predicate
>fetchRequest.sortDescriptors = sorters
>do {
>items = try fetchRequest.execute() as [Entity]
>} catch {
> 
>}
>}
>return items
>}
> }
> 
> @objc(Event)
> public class Event: NSManagedObject {
> 
> }
> 
> extension Event: DDRCoreData {
>typealias Entity = Event
> }
> 
> // compiler says items is of type [Event.Entity]
> let items = Event.items(in: controller.managedObjectContext!)
> 
> Thanks,
> Dave Reed
> 
> 
> ___
> 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] Using Github's Atom as a Swift IDE for Linux and Mac

2015-12-14 Thread Pierre Monod-Broca via swift-users
Oh great ! Thanks for sharing !

-- 
Pierre

> Le 12 déc. 2015 à 18:28, Ankit Agarwal via swift-users 
>  a écrit :
> 
> Hey
> 
> I wrote a tiny atom plugin which enables building and debugging swift 
> packages (using SPM) from inside atom. 
> Here is the blog post with tutorial 
> https://medium.com/@Aciid/hacking-atom-to-create-a-swift-ide-that-runs-on-linux-and-mac-c7d9520a0fac#.ytedt4uh9
>  
> 
> 
> 
> -- 
> Ankit
> 
> 
>  ___
> 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] Capturing references to initializers?

2015-12-10 Thread Pierre Monod-Broca via swift-users
Hi,

I had the same problem with delegate methods (they all had the same prefix). I 
used lambda as a workaround, but I agree it is less elegant :

let b: = { Foo(customNumber: $0) }
let c: = { Foo(numberToInc: $0) }


Pierre

> Le 10 déc. 2015 à 23:41, Austin Zheng via swift-users  
> a écrit :
> 
> Hello Swift users,
> 
> I have a question about capturing references to initializers. You can do the 
> following right now:
> 
> struct Foo {
> let number : Int
> // This type has a single initializer
> init() {
> number = 10
> }
> }
> 
> let a  = Foo.init  // a's type: () -> Foo
> 
> So far so good. Now, let's add in a second initializer:
> 
> extension Foo {
> init(customNumber: Int) {
> number = customNumber
> }
> }
> 
> Now, if you want to capture a reference to one of the initializers, you can 
> annotate the variable with the explicit function type:
> 
> let a : () -> Foo = Foo.init
> let b : Int -> Foo = Foo.init
> 
> My question involves the case where you have multiple initializers that take 
> the same arguments and types. How would you capture initializers then?
> 
> extension Foo {
> init(numberToInc: Int) {
> number = numberToInc + 1
> }
> }
> 
> How do I capture a reference to the numberToInc: initializer, versus the 
> customNumber: initializer?
> 
> Best regards,
> Austin
>  ___
> 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