why I can't access my 3rd level CoreData data in swift?
The temas
property is a NSOrderedSet
, and the subscript [0]
returns the
type AnyObject
. So (as already said in a comment) you have to cast the AnyObject
to the actual type
let curso = cursos[index]
println(curso.nombre)
let firstTema = curso.temas[0] as Tema
println(firstTema.nombre)
let firstSubTema = firstTema.subTemas[0] as SubTema
println(firstSubTema.nombre)
Note that you can simplify your loop using for - in
:
for curso in cursos {
// ...
}
and recursively enumerating all objects would look like
for curso in cursos {
println(curso.nombre)
for tema in curso.temas.array as [Tema] {
println(tema.nombre)
for subtema in tema.subTemas.array as [SubTema] {
println(subtema.nombre)
}
}
}
Core Data error when fetching
You probably changed your managed object model in Xcode, but you have still installed app on your device/simulator with older data model configuration.
That's why you getting this error.
If your app is brand new and you don't have to bother with compatibility to already installed apps, you can just erase old app from the device/simulator and install new one.
If you need to keep compatibility of older data model configuration with newer one, you should use versioning of the model and make all your changes in the newer data model version.
Why Xcode does not show index options for CoreData entities and attributes?
As @tomharrington said, the solution is in the WWDC 2017 video: Indexing stuff begins at 10:32 and the demo at 16:40.
It's still possible to create and index via Xcode9:
- First, select your entity, long-click on the + (Add Entity), and select Add Fetch Index.
Then, name your index appropriately, the select the property you want to index on (and choose ascending or descending, depending on how is organized your data)
Finally, as explained on the video, this will not trigger a migration and user that update your app will not get your index. If you want to force a model migration, add an Hash Modifier by selecting the entity, and setting Right Panel > 3rd tab > Versioning > Hash Modifier.
Saving CoreData to a Web Server with Swift 3.0
The question that you have linked is not trying to explain how to communicate with a web server. It is explaining how to store data in core data and tag/mark it in a way that you know which records have been sent to the web server or not.
So the Predicate will fetch all records that have not been sent to the web server and allow you to send them when you have an internet connection available.
Communicating with a web server can be a broad topic and will depend on your web server and API setup, so it is too much to explain here fully. I refer you to some free online resources that will help you understand networking in Swift.
- Udacity - Networking with Swift
- Ray Wenderlich - Alamofire Tutorial
- Stack Overflow - Swift POST Request
Here is an example of a POST Request from the StackOverflow answer above
var request = URLRequest(url: URL(string: "http://test.tranzporthub.com/street45/customer_login.php")!)
request.httpMethod = "POST"
let postString = "user_id=chaitanya3191@gmail.com&password=123"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString)")
}
task.resume()
Using code similar to this, you should be able to send data to your web server, then your web server can do whatever it likes with it.
UPDATE:
To encode your parameters to JSON you can use the following code as a guide
var dictionary = [
"username": "Test User",
"password": "Password"
]
if let jsonData = try? JSONSerialization.data(withJSONObject: dictionary, options: []) {
// jsonData is a byte sequence, to view it you would need to convert to string
print(String(bytes: jsonData, encoding: String.Encoding.utf8))
}
Which would output:
Optional("{\"username\":\"Test User\",\"password\":\"Password\"}")
Note: you would send it as data, not the string version. so your code might look like this:
request.httpBody = jsonData
Exception on Core Data save
NONE
is an alias for NOT ANY
and is indeed for to-many relationships.
What you probably want is:
[NSPredicate predicateWithFormat:@"NOT name in %@", excludedTypes];
Related Topics
iOS - Could Not Open Obj File When Convert Mdlasset to Mdlmesh
How to Get an Array of Days Between Two Dates in Swift
How to Calculate a Distance Between Two Anchorentities
Prefix(_ Maxlength:) Is Type-Erased When Used with a Struct That Conforms to Lazysequenceprotocol
Redirect Process Stdout to Apple System Log Facility in Swift
Swift Spritekit Get Visible Frame Size
Difference Between Orientation and Rotation in Scnnode
Changing Associated Value of Enum Swift
Arkit: How to Tell If User's Face Is Parallel to Camera
Prepareforsegue from a UIbutton in a Custom Prototype Cell
How to Programmatically Scroll iOS Wkwebview, Swift 4
Swiftui - Form with Error Message on Button Press and Navigation
Keep Getting an Error: "Uiimagepickercontroller Extension Discovery Failed with Error: (Null)"
Cloudkit - What to Do When a User Adds, Modifies or Deletes an Object While Offline
How to Download Datas from Multiple Url in Concurrency Mode
Creating Semaphore with Initial Value of 0 Make Issues with Execution