"When I tried to download and use the following sample code of ""06_Generating and Initializing Model Objects"" in the book ""Introduction to Realm"", I opened it in Xcode 9.1, but an error occurred and I couldn't move forward."Could you tell me how to solve this problem?
ViewController.swift/import RealmSwift in ModelCreation
Module compiled with Swift 3.0.2 cannot be imported in Swift 3.2.2
I searched online and tried the following 4 items.
①carriage bootstrap--platform iOS--no-use-binaries
(Results → -bash:carthage:command not found
)
②$brew update
/$brew install charge
(result →command not found
)
③Carthage.pkg
After downloading and installing ( (Result→
command not found
)
④After cleaning the build holder
, ( (result→command not found
)
Environment MacBookPro HighSierra 10.13.1 Xcode 9.1
import UIKit
import RealmSwift
class Person:Object {
dynamic var name=""
dynamic range = 0
dynamic var dog —One-to-one association with the Dog?// Dog model
Letcats=List<Cat>()// Associate with Cat Model One-to-Many
}
class Animal:Object {
dynamic var name=""
dynamic range = 0
}
class Dog:Animal {
}
class Cat:Animal {
}
classViewController:UIViewController {
override func viewDidLoad(){
super.viewDidLoad()
// MARK: How to Generate and Initialize -6.2
print ("6.2 Generation and Initialization Method (p035)")
print("-1 Set each property value after generation (p036)")
vardog = Dog()
dog.name = "Momo"
dog.age = 9
print("-2 Initialize with an object that complies with Key Value Coding (KVC) (p036)")
dog=Dog(value:["name":"Momo", "age":9]) // Initialize with Dictionary
print("-3 Initialize with an array of values for each property (p036)")
dog = Dog (value: ["Momo", 9])
// MARK: -6.3 Generating and Initializing Nested Model Objects
print("6.3 Generating and Initializing Nested Model Objects (p037)")
let cat1 = Cat(value: ["Toto", 1])
let cat2 = Cat(value: ["Rao", 2])
print("-1 Set each property value after generation (p037)")
var person = Person()
person.name = "Yu"
person.age = 32
person.dog = dog
person.cats.append (objectsIn: [cat1, cat2])
print("-2 Initialize with an object that complies with Key Value Coding (KVC) (p037)")
person=Person(value:["name":"Yu",
"age"—32,
"dog": ["name": "Momo", "age":9],
"cats": [[["name": "Toto", "age":1],
["name":"Rao", "age":2]]])
// Object also complies with key value coding and can be used for initialization.
person=Person(value:["name":"Yu",
"age"—32,
"dog"—dog,
"cats": [cat1, cat2]])
print("-3 Initialize with an array of values for each property (p038)")
person=Person(value:["Yu",
32,
dog,
[cat1, cat2])
// You cannot omit values in an array even if there are no optional types or elements in the List class.
// If there is no value, you must specify nil.
person=Person(value:["Yu",
32,
nil,
nil] as [Any?])
// For example, the following code crashes because there are no elements for the optional type and the list type.
// person=Person(value:["Yu", 32])//←Exception and crash when executed
}
// MARK: -Util
@IBOutlet weak var textView: UITextView!
func print(_string:String){
Swift.print(string)
if textView.text.characters.count>0{
textView.text=textView.text.appending("\n")
}
textView.text=textView.text.appending(string)
}
}
🙆 Resolved.
When I moved the directory of this project to a different location from the directory where I downloaded it from the sample source, I opened the project, removed the Realm and RealmSwift framework, installed the Realm and RealmSwift framework for my Xcode version, and it worked on simulator.
The installation followed Chapter 3 Realm Installation (Dynamic Framework Installation) in this book.
👍 I thought there was a version difference in the compilation stage, but I understood that some of the framework itself supports the Xcode version.It was not a charge issue.
© 2024 OneMinuteCode. All rights reserved.