Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Native Swift Set in Swift template (Swift 1.2) #266

Closed
sorted-bits opened this issue Feb 10, 2015 · 12 comments
Closed

Native Swift Set in Swift template (Swift 1.2) #266

sorted-bits opened this issue Feb 10, 2015 · 12 comments

Comments

@sorted-bits
Copy link

I am thinking about creating a pull-request to get mogenerator to work with Swift 1.2, since Swift 1.2 supports sets natively.

Only I am not really sure if the way I want to do it is the way it should be solved.

Currently the one-to-many relationships are defined as:

    @NSManaged
    var myObjects: NSSet

and there is a addMyObjects(objects: NSSet) method implemented like this:

    func addMyObjects(objects: NSSet) {
        let mutable = self.myObjects.mutableCopy() as NSMutableSet
        mutable.unionSet(objects)
        self.myObjects = mutable.copy() as NSSet
    }

I was taking a look at the new Swift 1.2 specs and it seems that we can now do:

    @NSManaged
    var myObjects: Set<MyObject>

and implement the addMyObjects method like this:

    func addMyObjects(objects: Set<MyObject>) {
        self.myObjects = self.myObjects.union(objects);
    }

    func removeMyObjects(objects: Set< MyObject>) {
        self.myObjects = self.myObjects.subtract(objects);
    }

As you can see, I removed the whole mutableCopy part, but I am not sure this is the way we should actually fix this for Swift 1.2.

I didn't use a let because the @NSManaged tag isn't allowed on that.

@sorted-bits
Copy link
Author

Created a pull request that uses Swift 1.2 Set objects. This is just a suggestion and might not be the way you want to solve Swift 1.2 support.

#267

@sorted-bits
Copy link
Author

As a set does not keep the order of the objects in the same way, it might even be nicer to use an Array? This might be worth a little discussion?

@dimazen
Copy link

dimazen commented Feb 12, 2015

@depl0y
For ordered relationship there is a NSOrderedSet. (however yep, there is no way to do for it)
Also Array doesn't provide uniqueness, which is important as well.

Generally speaking order of objects in Array will be random, because source is always a Set

@sorted-bits
Copy link
Author

@dimazen

Yup, I think using a NSOrderedSet would be hard, because there is no way to specify in the model editor, that you want an ordered set.

I forgot about the uniqueness, yes, so an Array is out of the question and just using the Set would probably be best. I hoped it could shorten the way to a 'static' list of objects, always in the same order.

@dimazen
Copy link

dimazen commented Feb 12, 2015

@depl0y actually there is! ;) review model editor for to-many relationship one more time. There is a "ordered" checkmark.

@sorted-bits
Copy link
Author

Ah yes, but it seems that Swift does not (yet) support the OrderedSet.

New native Set data structure — An unordered collection of unique elements that bridges with NSSet and provides value semantics like Array and Dictionary.

The current Swift template does not use that setting either (because of the lack of support for the ordered set maybe, although the current template uses NSSet, so there is no reason not to use NSOrderedSet there either).

The current implementation for Set<T> is also quite limited, compared to the objC equivalent. There is no filtering implemented. But I might be missing a couple of points there, because of the lack of documentation.

@sorted-bits
Copy link
Author

Ok, so I had a small discussion with somebody from Apple about the use of NSSet/Set to fill an UITableView. The person from Apple said it wouldn't be advisable to use a Set directly, because of it's inefficiency while iterating over it.

So, it is probably better to cast it to an Array before doing that, but I am wondering what the reason is that we use a Set here, instead of an Array (seriously, I don't know).

@andriyslyusar
Copy link

Hi,

Can it be that this issue: magicalpanda/MagicalRecord#963 related to using Set vs Array? I am currently using template from #267.

@andriyslyusar
Copy link

MagicalPanda issue was not related to this.

@justin
Copy link
Collaborator

justin commented Dec 26, 2015

I don't believe Core Data can handle swift native sets yet? Can someone point me to docs proving me wrong? I'd love to integrate them if its possible in Swift 2.0.

@MsrButterfly
Copy link

This is my project which using Core Data integrated with Swift 2.0 native Set. The feature is first available in Swift 1.2.
https://github.com/ifLab/WeCenterMobile-iOS

发送自 Outlook Mobile

On Sat, Dec 26, 2015 at 8:48 AM -0800, "Justin Williams" notifications@github.com wrote:

I don't believe Core Data can handle swift native sets yet? Can someone point me to docs proving me wrong? I'd love to integrate them if its possible in Swift 2.0.


Reply to this email directly or view it on GitHub.

@justin
Copy link
Collaborator

justin commented Mar 22, 2016

We are declaring pull request and issue 0 now that 1.3 is out. If this is still an issue you'd like to see address with 1.30 and going forward, please open a new issue so we can start a fresh discussion. Thank you!

@justin justin closed this as completed Mar 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants