How to use NSSet created from Core Data












0















I have the following core data model:
enter image description hereenter image description here



where Person to Codes is a one-to-many relationship.



I have a function which returns a Person record and if the code person.codes returns an NSSet of all the codes associated with that Person. The issue that I am having is how to use the NSSet.



person.codes.allObjects.first returns this data:



<Codes: 0x60000213cb40> (entity: Codes; id: 0xb978dbf34ddb849 <x-coredata://A2B634E4-E136-48E1-B2C5-82B6B68FBE44/Codes/p1> ; data: {
code = 4LQ;
number = 1;
whosAccount = "0xb978dbf34ddb869 <x-coredata://A2B634E4-E136-48E1-B2C5-82B6B68FBE44/Person/p1>";
})


I thought if I made person.codes.allObjects.first of type Codes, I would be able to access the code and number elements but I get an error: error: value of type 'Any?' has no member 'number'



Also, how can I search this data set for a particular code or number.



I appreciate that this is proabably a simple question but have searched and read the documentation to no avail. I suspect that may base knowledge is not sufficient.



Update



I have a CoreDataHandler class which contains the following code:



class CoreDataHandler: NSObject {
//static let sharedInstance = CoreDataHandler()

private static func getContext() -> NSManagedObjectContext {
let appDelegate = NSApplication.shared.delegate as! AppDelegate

return appDelegate.persistentContainer.viewContext
}

static func fetchPerson() -> [Person]? {
let context = getContext()

do {
let persons: [Person] = try context.fetch(Person.fetchRequest())
return persons
} catch {
return nil
}
}


I can fetch a person using:



let row = personTableView.selectedRow
let person = CoreDataHandler.fetchPerson()?[row]









share|improve this question

























  • All objects takes the NSSet and makes an array

    – pdoak
    Nov 22 '18 at 21:34











  • map it into an array first, if you need an index-based collection.

    – holex
    Nov 23 '18 at 13:07
















0















I have the following core data model:
enter image description hereenter image description here



where Person to Codes is a one-to-many relationship.



I have a function which returns a Person record and if the code person.codes returns an NSSet of all the codes associated with that Person. The issue that I am having is how to use the NSSet.



person.codes.allObjects.first returns this data:



<Codes: 0x60000213cb40> (entity: Codes; id: 0xb978dbf34ddb849 <x-coredata://A2B634E4-E136-48E1-B2C5-82B6B68FBE44/Codes/p1> ; data: {
code = 4LQ;
number = 1;
whosAccount = "0xb978dbf34ddb869 <x-coredata://A2B634E4-E136-48E1-B2C5-82B6B68FBE44/Person/p1>";
})


I thought if I made person.codes.allObjects.first of type Codes, I would be able to access the code and number elements but I get an error: error: value of type 'Any?' has no member 'number'



Also, how can I search this data set for a particular code or number.



I appreciate that this is proabably a simple question but have searched and read the documentation to no avail. I suspect that may base knowledge is not sufficient.



Update



I have a CoreDataHandler class which contains the following code:



class CoreDataHandler: NSObject {
//static let sharedInstance = CoreDataHandler()

private static func getContext() -> NSManagedObjectContext {
let appDelegate = NSApplication.shared.delegate as! AppDelegate

return appDelegate.persistentContainer.viewContext
}

static func fetchPerson() -> [Person]? {
let context = getContext()

do {
let persons: [Person] = try context.fetch(Person.fetchRequest())
return persons
} catch {
return nil
}
}


I can fetch a person using:



let row = personTableView.selectedRow
let person = CoreDataHandler.fetchPerson()?[row]









share|improve this question

























  • All objects takes the NSSet and makes an array

    – pdoak
    Nov 22 '18 at 21:34











  • map it into an array first, if you need an index-based collection.

    – holex
    Nov 23 '18 at 13:07














0












0








0








I have the following core data model:
enter image description hereenter image description here



where Person to Codes is a one-to-many relationship.



I have a function which returns a Person record and if the code person.codes returns an NSSet of all the codes associated with that Person. The issue that I am having is how to use the NSSet.



person.codes.allObjects.first returns this data:



<Codes: 0x60000213cb40> (entity: Codes; id: 0xb978dbf34ddb849 <x-coredata://A2B634E4-E136-48E1-B2C5-82B6B68FBE44/Codes/p1> ; data: {
code = 4LQ;
number = 1;
whosAccount = "0xb978dbf34ddb869 <x-coredata://A2B634E4-E136-48E1-B2C5-82B6B68FBE44/Person/p1>";
})


I thought if I made person.codes.allObjects.first of type Codes, I would be able to access the code and number elements but I get an error: error: value of type 'Any?' has no member 'number'



Also, how can I search this data set for a particular code or number.



I appreciate that this is proabably a simple question but have searched and read the documentation to no avail. I suspect that may base knowledge is not sufficient.



Update



I have a CoreDataHandler class which contains the following code:



class CoreDataHandler: NSObject {
//static let sharedInstance = CoreDataHandler()

private static func getContext() -> NSManagedObjectContext {
let appDelegate = NSApplication.shared.delegate as! AppDelegate

return appDelegate.persistentContainer.viewContext
}

static func fetchPerson() -> [Person]? {
let context = getContext()

do {
let persons: [Person] = try context.fetch(Person.fetchRequest())
return persons
} catch {
return nil
}
}


I can fetch a person using:



let row = personTableView.selectedRow
let person = CoreDataHandler.fetchPerson()?[row]









share|improve this question
















I have the following core data model:
enter image description hereenter image description here



where Person to Codes is a one-to-many relationship.



I have a function which returns a Person record and if the code person.codes returns an NSSet of all the codes associated with that Person. The issue that I am having is how to use the NSSet.



person.codes.allObjects.first returns this data:



<Codes: 0x60000213cb40> (entity: Codes; id: 0xb978dbf34ddb849 <x-coredata://A2B634E4-E136-48E1-B2C5-82B6B68FBE44/Codes/p1> ; data: {
code = 4LQ;
number = 1;
whosAccount = "0xb978dbf34ddb869 <x-coredata://A2B634E4-E136-48E1-B2C5-82B6B68FBE44/Person/p1>";
})


I thought if I made person.codes.allObjects.first of type Codes, I would be able to access the code and number elements but I get an error: error: value of type 'Any?' has no member 'number'



Also, how can I search this data set for a particular code or number.



I appreciate that this is proabably a simple question but have searched and read the documentation to no avail. I suspect that may base knowledge is not sufficient.



Update



I have a CoreDataHandler class which contains the following code:



class CoreDataHandler: NSObject {
//static let sharedInstance = CoreDataHandler()

private static func getContext() -> NSManagedObjectContext {
let appDelegate = NSApplication.shared.delegate as! AppDelegate

return appDelegate.persistentContainer.viewContext
}

static func fetchPerson() -> [Person]? {
let context = getContext()

do {
let persons: [Person] = try context.fetch(Person.fetchRequest())
return persons
} catch {
return nil
}
}


I can fetch a person using:



let row = personTableView.selectedRow
let person = CoreDataHandler.fetchPerson()?[row]






swift core-data nsset






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 12:50







pdoak

















asked Nov 22 '18 at 20:52









pdoakpdoak

339212




339212













  • All objects takes the NSSet and makes an array

    – pdoak
    Nov 22 '18 at 21:34











  • map it into an array first, if you need an index-based collection.

    – holex
    Nov 23 '18 at 13:07



















  • All objects takes the NSSet and makes an array

    – pdoak
    Nov 22 '18 at 21:34











  • map it into an array first, if you need an index-based collection.

    – holex
    Nov 23 '18 at 13:07

















All objects takes the NSSet and makes an array

– pdoak
Nov 22 '18 at 21:34





All objects takes the NSSet and makes an array

– pdoak
Nov 22 '18 at 21:34













map it into an array first, if you need an index-based collection.

– holex
Nov 23 '18 at 13:07





map it into an array first, if you need an index-based collection.

– holex
Nov 23 '18 at 13:07












2 Answers
2






active

oldest

votes


















1














Core Data supports widely native Swift types.



Declare codes as Set<Codes> in the Person class.



It's much more convenient than typeless NSSet.

You get a strong type and you can apply all native functions like filter, sort, etc. without type cast.






share|improve this answer


























  • I realize that it is probably an obvious question but how do I actually declare codes in the Person class?

    – pdoak
    Nov 23 '18 at 17:36











  • You have to create the NSManagedObject subclasses manually. In the model file in the Entity Inspector change the Codegen popup of all entities to Manual/None, then in Menu Editor > Create NSManagedObject Subclass create the classes and change the types.

    – vadian
    Nov 23 '18 at 17:40











  • I have done that and have a Person class which looks like this:public class Person: NSManagedObject { }. How do I declare codes in that class?

    – pdoak
    Nov 23 '18 at 17:45











  • There are two files per entity. A +CoreDataClass file and a +CoreDataProperties file. The properties are declared in the +CoreDataProperties file.

    – vadian
    Nov 23 '18 at 17:50











  • In my Person+.. file, I have: @NSManaged public var codes: Set<Codes>?. Now in my viewController class, this code works: let codes = person?.codes; let filteredCodes = codes!.filter({ $0.number == 1 }); print("key for code1= (filteredCodes)") and prints out: key for code1= [<Codes: 0x60000213d360> (entity: Codes; id: 0xe7b68aa93f25f785 <x-coredata://.........> ; data: { code = 4LQ; number = 1; whosAccount = "0xe7b68aa93f25f7a5 <x-coredata://.......>"; })]. But filteredCodes.code is not recognised. How do I get to just the code, 4LQ?

    – pdoak
    Nov 23 '18 at 18:03





















0














let codes = person.codes as! Set<Code>


Once that is done you can access the properties. Searching can be done by filtering for instance



let filteredCodes = codes.filter({ $0.code == "XYZ" })


will return all objects that has the code "XYZ". Or to get only one you can use



let code = codes.first(where: {$0.id == 1})


which will return the first object that has id = 1



A simple example getting all Person objects that has a given code



func findWithCode(_ code: String) -> [Person] {
guard let persons = CoreDataHandler.fetchPerson() else {
return
}

var result = [Person]()
for person in persons {
let codes = person.codes as! Set<Code>
if codes.contains(where: { $0.code == code }) {
result.append(person)
}
}

return persons
}





share|improve this answer


























  • I get this error when I try and typecast the set as Codes: Cast from 'NSSet?' to unrelated type 'Codes' always fails`

    – pdoak
    Nov 23 '18 at 8:05











  • Have you tried as a native type let codes: Set<Codes> = person.codes?

    – Joakim Danielson
    Nov 23 '18 at 8:40











  • If I try let codes: Set<Codes> = person.codes, I get this error: Cannot convert value of type 'NSSet?' to specified type 'Set<Codes>

    – pdoak
    Nov 23 '18 at 9:03











  • @pdoak My answer is incorrect in the typecasting, I'll update

    – Joakim Danielson
    Nov 23 '18 at 12:35













  • if I try let codes = person.codes as! [Codes], I get this error: Cast from 'NSSet?' to unrelated type '[Codes]' always fails

    – pdoak
    Nov 23 '18 at 12:58













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437853%2fhow-to-use-nsset-created-from-core-data%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Core Data supports widely native Swift types.



Declare codes as Set<Codes> in the Person class.



It's much more convenient than typeless NSSet.

You get a strong type and you can apply all native functions like filter, sort, etc. without type cast.






share|improve this answer


























  • I realize that it is probably an obvious question but how do I actually declare codes in the Person class?

    – pdoak
    Nov 23 '18 at 17:36











  • You have to create the NSManagedObject subclasses manually. In the model file in the Entity Inspector change the Codegen popup of all entities to Manual/None, then in Menu Editor > Create NSManagedObject Subclass create the classes and change the types.

    – vadian
    Nov 23 '18 at 17:40











  • I have done that and have a Person class which looks like this:public class Person: NSManagedObject { }. How do I declare codes in that class?

    – pdoak
    Nov 23 '18 at 17:45











  • There are two files per entity. A +CoreDataClass file and a +CoreDataProperties file. The properties are declared in the +CoreDataProperties file.

    – vadian
    Nov 23 '18 at 17:50











  • In my Person+.. file, I have: @NSManaged public var codes: Set<Codes>?. Now in my viewController class, this code works: let codes = person?.codes; let filteredCodes = codes!.filter({ $0.number == 1 }); print("key for code1= (filteredCodes)") and prints out: key for code1= [<Codes: 0x60000213d360> (entity: Codes; id: 0xe7b68aa93f25f785 <x-coredata://.........> ; data: { code = 4LQ; number = 1; whosAccount = "0xe7b68aa93f25f7a5 <x-coredata://.......>"; })]. But filteredCodes.code is not recognised. How do I get to just the code, 4LQ?

    – pdoak
    Nov 23 '18 at 18:03


















1














Core Data supports widely native Swift types.



Declare codes as Set<Codes> in the Person class.



It's much more convenient than typeless NSSet.

You get a strong type and you can apply all native functions like filter, sort, etc. without type cast.






share|improve this answer


























  • I realize that it is probably an obvious question but how do I actually declare codes in the Person class?

    – pdoak
    Nov 23 '18 at 17:36











  • You have to create the NSManagedObject subclasses manually. In the model file in the Entity Inspector change the Codegen popup of all entities to Manual/None, then in Menu Editor > Create NSManagedObject Subclass create the classes and change the types.

    – vadian
    Nov 23 '18 at 17:40











  • I have done that and have a Person class which looks like this:public class Person: NSManagedObject { }. How do I declare codes in that class?

    – pdoak
    Nov 23 '18 at 17:45











  • There are two files per entity. A +CoreDataClass file and a +CoreDataProperties file. The properties are declared in the +CoreDataProperties file.

    – vadian
    Nov 23 '18 at 17:50











  • In my Person+.. file, I have: @NSManaged public var codes: Set<Codes>?. Now in my viewController class, this code works: let codes = person?.codes; let filteredCodes = codes!.filter({ $0.number == 1 }); print("key for code1= (filteredCodes)") and prints out: key for code1= [<Codes: 0x60000213d360> (entity: Codes; id: 0xe7b68aa93f25f785 <x-coredata://.........> ; data: { code = 4LQ; number = 1; whosAccount = "0xe7b68aa93f25f7a5 <x-coredata://.......>"; })]. But filteredCodes.code is not recognised. How do I get to just the code, 4LQ?

    – pdoak
    Nov 23 '18 at 18:03
















1












1








1







Core Data supports widely native Swift types.



Declare codes as Set<Codes> in the Person class.



It's much more convenient than typeless NSSet.

You get a strong type and you can apply all native functions like filter, sort, etc. without type cast.






share|improve this answer















Core Data supports widely native Swift types.



Declare codes as Set<Codes> in the Person class.



It's much more convenient than typeless NSSet.

You get a strong type and you can apply all native functions like filter, sort, etc. without type cast.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 12:45

























answered Nov 22 '18 at 22:50









vadianvadian

152k16160186




152k16160186













  • I realize that it is probably an obvious question but how do I actually declare codes in the Person class?

    – pdoak
    Nov 23 '18 at 17:36











  • You have to create the NSManagedObject subclasses manually. In the model file in the Entity Inspector change the Codegen popup of all entities to Manual/None, then in Menu Editor > Create NSManagedObject Subclass create the classes and change the types.

    – vadian
    Nov 23 '18 at 17:40











  • I have done that and have a Person class which looks like this:public class Person: NSManagedObject { }. How do I declare codes in that class?

    – pdoak
    Nov 23 '18 at 17:45











  • There are two files per entity. A +CoreDataClass file and a +CoreDataProperties file. The properties are declared in the +CoreDataProperties file.

    – vadian
    Nov 23 '18 at 17:50











  • In my Person+.. file, I have: @NSManaged public var codes: Set<Codes>?. Now in my viewController class, this code works: let codes = person?.codes; let filteredCodes = codes!.filter({ $0.number == 1 }); print("key for code1= (filteredCodes)") and prints out: key for code1= [<Codes: 0x60000213d360> (entity: Codes; id: 0xe7b68aa93f25f785 <x-coredata://.........> ; data: { code = 4LQ; number = 1; whosAccount = "0xe7b68aa93f25f7a5 <x-coredata://.......>"; })]. But filteredCodes.code is not recognised. How do I get to just the code, 4LQ?

    – pdoak
    Nov 23 '18 at 18:03





















  • I realize that it is probably an obvious question but how do I actually declare codes in the Person class?

    – pdoak
    Nov 23 '18 at 17:36











  • You have to create the NSManagedObject subclasses manually. In the model file in the Entity Inspector change the Codegen popup of all entities to Manual/None, then in Menu Editor > Create NSManagedObject Subclass create the classes and change the types.

    – vadian
    Nov 23 '18 at 17:40











  • I have done that and have a Person class which looks like this:public class Person: NSManagedObject { }. How do I declare codes in that class?

    – pdoak
    Nov 23 '18 at 17:45











  • There are two files per entity. A +CoreDataClass file and a +CoreDataProperties file. The properties are declared in the +CoreDataProperties file.

    – vadian
    Nov 23 '18 at 17:50











  • In my Person+.. file, I have: @NSManaged public var codes: Set<Codes>?. Now in my viewController class, this code works: let codes = person?.codes; let filteredCodes = codes!.filter({ $0.number == 1 }); print("key for code1= (filteredCodes)") and prints out: key for code1= [<Codes: 0x60000213d360> (entity: Codes; id: 0xe7b68aa93f25f785 <x-coredata://.........> ; data: { code = 4LQ; number = 1; whosAccount = "0xe7b68aa93f25f7a5 <x-coredata://.......>"; })]. But filteredCodes.code is not recognised. How do I get to just the code, 4LQ?

    – pdoak
    Nov 23 '18 at 18:03



















I realize that it is probably an obvious question but how do I actually declare codes in the Person class?

– pdoak
Nov 23 '18 at 17:36





I realize that it is probably an obvious question but how do I actually declare codes in the Person class?

– pdoak
Nov 23 '18 at 17:36













You have to create the NSManagedObject subclasses manually. In the model file in the Entity Inspector change the Codegen popup of all entities to Manual/None, then in Menu Editor > Create NSManagedObject Subclass create the classes and change the types.

– vadian
Nov 23 '18 at 17:40





You have to create the NSManagedObject subclasses manually. In the model file in the Entity Inspector change the Codegen popup of all entities to Manual/None, then in Menu Editor > Create NSManagedObject Subclass create the classes and change the types.

– vadian
Nov 23 '18 at 17:40













I have done that and have a Person class which looks like this:public class Person: NSManagedObject { }. How do I declare codes in that class?

– pdoak
Nov 23 '18 at 17:45





I have done that and have a Person class which looks like this:public class Person: NSManagedObject { }. How do I declare codes in that class?

– pdoak
Nov 23 '18 at 17:45













There are two files per entity. A +CoreDataClass file and a +CoreDataProperties file. The properties are declared in the +CoreDataProperties file.

– vadian
Nov 23 '18 at 17:50





There are two files per entity. A +CoreDataClass file and a +CoreDataProperties file. The properties are declared in the +CoreDataProperties file.

– vadian
Nov 23 '18 at 17:50













In my Person+.. file, I have: @NSManaged public var codes: Set<Codes>?. Now in my viewController class, this code works: let codes = person?.codes; let filteredCodes = codes!.filter({ $0.number == 1 }); print("key for code1= (filteredCodes)") and prints out: key for code1= [<Codes: 0x60000213d360> (entity: Codes; id: 0xe7b68aa93f25f785 <x-coredata://.........> ; data: { code = 4LQ; number = 1; whosAccount = "0xe7b68aa93f25f7a5 <x-coredata://.......>"; })]. But filteredCodes.code is not recognised. How do I get to just the code, 4LQ?

– pdoak
Nov 23 '18 at 18:03







In my Person+.. file, I have: @NSManaged public var codes: Set<Codes>?. Now in my viewController class, this code works: let codes = person?.codes; let filteredCodes = codes!.filter({ $0.number == 1 }); print("key for code1= (filteredCodes)") and prints out: key for code1= [<Codes: 0x60000213d360> (entity: Codes; id: 0xe7b68aa93f25f785 <x-coredata://.........> ; data: { code = 4LQ; number = 1; whosAccount = "0xe7b68aa93f25f7a5 <x-coredata://.......>"; })]. But filteredCodes.code is not recognised. How do I get to just the code, 4LQ?

– pdoak
Nov 23 '18 at 18:03















0














let codes = person.codes as! Set<Code>


Once that is done you can access the properties. Searching can be done by filtering for instance



let filteredCodes = codes.filter({ $0.code == "XYZ" })


will return all objects that has the code "XYZ". Or to get only one you can use



let code = codes.first(where: {$0.id == 1})


which will return the first object that has id = 1



A simple example getting all Person objects that has a given code



func findWithCode(_ code: String) -> [Person] {
guard let persons = CoreDataHandler.fetchPerson() else {
return
}

var result = [Person]()
for person in persons {
let codes = person.codes as! Set<Code>
if codes.contains(where: { $0.code == code }) {
result.append(person)
}
}

return persons
}





share|improve this answer


























  • I get this error when I try and typecast the set as Codes: Cast from 'NSSet?' to unrelated type 'Codes' always fails`

    – pdoak
    Nov 23 '18 at 8:05











  • Have you tried as a native type let codes: Set<Codes> = person.codes?

    – Joakim Danielson
    Nov 23 '18 at 8:40











  • If I try let codes: Set<Codes> = person.codes, I get this error: Cannot convert value of type 'NSSet?' to specified type 'Set<Codes>

    – pdoak
    Nov 23 '18 at 9:03











  • @pdoak My answer is incorrect in the typecasting, I'll update

    – Joakim Danielson
    Nov 23 '18 at 12:35













  • if I try let codes = person.codes as! [Codes], I get this error: Cast from 'NSSet?' to unrelated type '[Codes]' always fails

    – pdoak
    Nov 23 '18 at 12:58


















0














let codes = person.codes as! Set<Code>


Once that is done you can access the properties. Searching can be done by filtering for instance



let filteredCodes = codes.filter({ $0.code == "XYZ" })


will return all objects that has the code "XYZ". Or to get only one you can use



let code = codes.first(where: {$0.id == 1})


which will return the first object that has id = 1



A simple example getting all Person objects that has a given code



func findWithCode(_ code: String) -> [Person] {
guard let persons = CoreDataHandler.fetchPerson() else {
return
}

var result = [Person]()
for person in persons {
let codes = person.codes as! Set<Code>
if codes.contains(where: { $0.code == code }) {
result.append(person)
}
}

return persons
}





share|improve this answer


























  • I get this error when I try and typecast the set as Codes: Cast from 'NSSet?' to unrelated type 'Codes' always fails`

    – pdoak
    Nov 23 '18 at 8:05











  • Have you tried as a native type let codes: Set<Codes> = person.codes?

    – Joakim Danielson
    Nov 23 '18 at 8:40











  • If I try let codes: Set<Codes> = person.codes, I get this error: Cannot convert value of type 'NSSet?' to specified type 'Set<Codes>

    – pdoak
    Nov 23 '18 at 9:03











  • @pdoak My answer is incorrect in the typecasting, I'll update

    – Joakim Danielson
    Nov 23 '18 at 12:35













  • if I try let codes = person.codes as! [Codes], I get this error: Cast from 'NSSet?' to unrelated type '[Codes]' always fails

    – pdoak
    Nov 23 '18 at 12:58
















0












0








0







let codes = person.codes as! Set<Code>


Once that is done you can access the properties. Searching can be done by filtering for instance



let filteredCodes = codes.filter({ $0.code == "XYZ" })


will return all objects that has the code "XYZ". Or to get only one you can use



let code = codes.first(where: {$0.id == 1})


which will return the first object that has id = 1



A simple example getting all Person objects that has a given code



func findWithCode(_ code: String) -> [Person] {
guard let persons = CoreDataHandler.fetchPerson() else {
return
}

var result = [Person]()
for person in persons {
let codes = person.codes as! Set<Code>
if codes.contains(where: { $0.code == code }) {
result.append(person)
}
}

return persons
}





share|improve this answer















let codes = person.codes as! Set<Code>


Once that is done you can access the properties. Searching can be done by filtering for instance



let filteredCodes = codes.filter({ $0.code == "XYZ" })


will return all objects that has the code "XYZ". Or to get only one you can use



let code = codes.first(where: {$0.id == 1})


which will return the first object that has id = 1



A simple example getting all Person objects that has a given code



func findWithCode(_ code: String) -> [Person] {
guard let persons = CoreDataHandler.fetchPerson() else {
return
}

var result = [Person]()
for person in persons {
let codes = person.codes as! Set<Code>
if codes.contains(where: { $0.code == code }) {
result.append(person)
}
}

return persons
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 21:17

























answered Nov 22 '18 at 21:36









Joakim DanielsonJoakim Danielson

9,8023725




9,8023725













  • I get this error when I try and typecast the set as Codes: Cast from 'NSSet?' to unrelated type 'Codes' always fails`

    – pdoak
    Nov 23 '18 at 8:05











  • Have you tried as a native type let codes: Set<Codes> = person.codes?

    – Joakim Danielson
    Nov 23 '18 at 8:40











  • If I try let codes: Set<Codes> = person.codes, I get this error: Cannot convert value of type 'NSSet?' to specified type 'Set<Codes>

    – pdoak
    Nov 23 '18 at 9:03











  • @pdoak My answer is incorrect in the typecasting, I'll update

    – Joakim Danielson
    Nov 23 '18 at 12:35













  • if I try let codes = person.codes as! [Codes], I get this error: Cast from 'NSSet?' to unrelated type '[Codes]' always fails

    – pdoak
    Nov 23 '18 at 12:58





















  • I get this error when I try and typecast the set as Codes: Cast from 'NSSet?' to unrelated type 'Codes' always fails`

    – pdoak
    Nov 23 '18 at 8:05











  • Have you tried as a native type let codes: Set<Codes> = person.codes?

    – Joakim Danielson
    Nov 23 '18 at 8:40











  • If I try let codes: Set<Codes> = person.codes, I get this error: Cannot convert value of type 'NSSet?' to specified type 'Set<Codes>

    – pdoak
    Nov 23 '18 at 9:03











  • @pdoak My answer is incorrect in the typecasting, I'll update

    – Joakim Danielson
    Nov 23 '18 at 12:35













  • if I try let codes = person.codes as! [Codes], I get this error: Cast from 'NSSet?' to unrelated type '[Codes]' always fails

    – pdoak
    Nov 23 '18 at 12:58



















I get this error when I try and typecast the set as Codes: Cast from 'NSSet?' to unrelated type 'Codes' always fails`

– pdoak
Nov 23 '18 at 8:05





I get this error when I try and typecast the set as Codes: Cast from 'NSSet?' to unrelated type 'Codes' always fails`

– pdoak
Nov 23 '18 at 8:05













Have you tried as a native type let codes: Set<Codes> = person.codes?

– Joakim Danielson
Nov 23 '18 at 8:40





Have you tried as a native type let codes: Set<Codes> = person.codes?

– Joakim Danielson
Nov 23 '18 at 8:40













If I try let codes: Set<Codes> = person.codes, I get this error: Cannot convert value of type 'NSSet?' to specified type 'Set<Codes>

– pdoak
Nov 23 '18 at 9:03





If I try let codes: Set<Codes> = person.codes, I get this error: Cannot convert value of type 'NSSet?' to specified type 'Set<Codes>

– pdoak
Nov 23 '18 at 9:03













@pdoak My answer is incorrect in the typecasting, I'll update

– Joakim Danielson
Nov 23 '18 at 12:35







@pdoak My answer is incorrect in the typecasting, I'll update

– Joakim Danielson
Nov 23 '18 at 12:35















if I try let codes = person.codes as! [Codes], I get this error: Cast from 'NSSet?' to unrelated type '[Codes]' always fails

– pdoak
Nov 23 '18 at 12:58







if I try let codes = person.codes as! [Codes], I get this error: Cast from 'NSSet?' to unrelated type '[Codes]' always fails

– pdoak
Nov 23 '18 at 12:58




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437853%2fhow-to-use-nsset-created-from-core-data%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?