Here is an issue I have with this case of code:
let entityTypeDico = ["entityTypeOne": arrayOne, "entityTypeOneTwo": arrayTwo,
"entityTypeThree": arrayThree, "entityTypeFour": arrayFour];
for (entityName, arrayItem) in entityTypeDico {
for x in arrayItem {
/*do something with x and arrayItem
I need to use the class of entityName */
//To get it I tried to use some code like:
NSClassFromString(entityName) // But it does not work.
}
}
entityTypeOne, ..... entityTypeFour are names of CoreData entities,
arrayOne, ..... arrayFour are some kind of arrays (not so important for the question).
What exact code do I need to use to get hold of the type of each entity inside the loop?
Your second for loop doesnt make sense. Can you try?
for (entityName, arrayItem) in entityTypeDico {
NSClassFromString("YourProjectName.\(entityName)")
}
Edit 1
Currently I am working on a project where I used NSClassFromString. And that is how I used it in my code.
In my main class I use:
let carClass = NSClassFromString("MyProjectName.\(self.selectedBrand)\(self.selectedModel)") as! NSObject.Type
_ = carClass.init()
where selectedBrand and selectedModel are picked by the user.
In the car class, I have an init function
override init() {
super.init()
// My codes here like initializing webview
}
Hope it helps
Here what I ended up by finding after a number of experiments.
It works and does what I want. I hope it will be useful to some other people too.
let entityTypeDico = ["entityTypeOne": arrayOne, "entityTypeOneTwo": arrayTwo,
"entityTypeThree": arrayThree, "entityTypeFour": arrayFour];
// "entityTypeXYZ" is a class name (subclass of NSManagedObject)
// arrayXYZ is some array (not so important for the question)
for (entityName, arrayItem) in entityTypeDico {
for x in arrayItem {
/*do something with x and arrayItem
I need to use the class of entityName */
//To get it I tried to use some code like:
NSClassFromString(entityName) // But it did not work.
// Here is what finally worked for me:
((NSClassFromString("\(entityName)")) as! NSManagedObject.Type)
}
}
Related
Now I using Realm to create a singleton, but I faced a problem, if I want to return a closure in the generic, and there are different types (Ex:.Case type return [Case], .Product type return [Product]), but I have no idea to implement this fuction.
Could anyone help me with this problem?Thanks!
DataType:
enum DataType{
case Case
case Product
case Category
case Composition
}
fetchItem fuction:
func fetchItem<T>(type: DataType,complete:#escaping ([T]) -> ()) {
var list:[T]? = []
switch type {
case .Case:
let realm = try! Realm()
let caseResult = realm.objects(Case.self)
caseResult.forEach { (item) in
...
}
complete([])
return
case .Product:
return
case .Category:
return
case .Composition:
return
}
}
Templating a function is the class that you want to get.
Let's take for exemple this function
func showTemplate<T>(myVariable: T) {
print(myVariable)
}
Here, if I want to call the showTemplate function I must do it with the type I want :
showTemplate<String>() # print a string
showTemplate<Int>() # print a int
showTemplate<myClass> # print a myClass
So you are having a problem, but in the wrong way because with templating, you HAVE to know the class before you call the function.
You can for example try to use inheritance and templating your motherClass with your wanted class.
class wantedClass {
var myCaseVariable: Case
var myProductVariable: Product
var myThingsVariable: Things
init() {
}
fillClass<T: Case>() {
// set case
}
// Etc etc
}
Moreover, I don't think that templating is the good solution here, I suggest to look at this : Using a Type Variable in a Generic
I believe you cannot do this for completely different types in one place using only generic type (not any class-holders or so). Maybe you should create separate funcs for each item (fetchCaseItems(), fetchProductItems, etc.). It's much clear to read and every func is responsible only for its own data type.
I have a Models class defined like this:
class BaseModel: Object {
var data: JSON = JSON.null
convenience init(_ data: JSON) {
self.init()
self.data = data
}
override static func ignoredProperties() -> [String] {
return ["data"]
}
}
class RecipeModel: BaseModel {
dynamic var title: String {
get { return data["fields"]["title"].stringValue }
set { self.title = newValue }
}
... more vars ...
var ingredients: List<IngredientsModel> {
get {
let ingredients = List<IngredientsModel>()
for item in data["fields"]["ingredients"] {
ingredients.append(IngredientsModel(item.1))
}
return ingredients
}
set { self.ingredients = newValue }
}
}
class IngredientsModel: BaseModel {
dynamic var text: String {
get { return data["text"].stringValue }
set { self.text = newValue }
}
... more vars ...
}
And I would like to use it something like this:
Api.shared.fetchAllEntries().call(onSuccess: {response in
print(response.json)
let realm = try! Realm()
try! realm.write {
realm.deleteAll()
}
for item in response.json["items"].arrayValue {
let recipe = RecipeModel(item)
try! realm.write {
realm.add(recipe)
}
}
}, onError: {
print("error")
})
So basically the idea is to just pass the whole JSON to the initial RecipeModel class, and it should parse it out and create the objects I need in the Realm database. It works quite well except for the nested list of IngredientsModel. They do not get added to the realm database.
What I see as a potential problem is that I call self.init() before I call self.data in the convenience init, but I do not see any way to work around this. Do you guys please know how I could achieve that also the IngredientsModel would have its contents set up properly and I would have a list of ingredients in the RecipeModel?
Your current implementation doesn't work, because you are not calling the getter/setter of ingredients in the init method of RecipeModel and hence the IngredientsModel instances are never persisted in Realm.
Moreover, using a computed property as a one-to-many relationship (Realm List) is a really bad idea, especially if you are parsing the results inside the getter for this property. Every time you call the getter of ingredients, you create new model objects instead of just accessing the existing ones that are already stored in Realm, but you are never deleting the old ones. If you were actually saving the IngredientsModel instances to Realm (which you don't do at the moment as mentioned above) you would see that your database is full of duplicate entries.
Your whole approach seems really suboptimal. You shouldn't store the unparsed data object in your model class and use computed properties to parse it. You should parse it when initializing your models and shouldn't store the unparsed data at all. You can use the ObjectMapper library for creating Realm objects straight away from the JSON response.
This question already has an answer here:
No more `private init` in Swift?
(1 answer)
Closed 6 years ago.
What ways do you know to prevent an object construction using Swift programming language?
In C++ I can simply make the constructor private like this:
struct A {
private:
A() {};
};
int main()
{
// Doesn't compile because the constructor is private.
A obj;
return 0;
}
When I do a similar thing in Swift (I tried it in playground) the code compiles just fine:
class A {
private init() {}
}
let obj = A()
UPDATE:
Ok, this question is marked as a duplicate. But I think this is a misunderstanding. What I'm asking about is what are the best practices you know to prevent object construction in Swift. All I want to achieve is to make it clear to the users of my class that it should not be constructible.
UPDATE 2:
As this question is still here, I think, it needs some more clarifications for those who still can't comprehend what I really want.
Given a class that is used as a wrapper for some useful constants such as the following:
class Constants {
static let someConstant1 = "CONSTANT_VALUE1"
static let someConstant2 = "CONSTANT_VALUE2"
//....etc...
}
what option can be considered as a best practice:
Leave it as is and don't worry about the possibility of objects creation outside this class;
Add private init() {} to prevent creation of the objects outside the current file;
Use init? and return nil to indicate that the objects must not be created as was suggested in the comments.
Hope the question is more clear now.
From Apple's guide to Swift:
Private access restricts the use of an entity to its own defining
source file. Use private access to hide the implementation details of
a specific piece of functionality.
Your playground file is all one file, so privacy is not enforced.
For instance, if you create a new project and add a file called Dog.swift to the project that looks like this:
import Foundation
class Dog {
private init() {
print("hello")
}
}
class Cat {
var d = Dog()
}
in ViewController.swift, you can write:
override func viewDidLoad() {
let c = Cat() //=>hello
}
But, if you try:
override func viewDidLoad() {
let d = Dog()
}
Xcode will flag that as an error before you even compile the program:
'Dog' cannot be constructed because it has no accessible initializers
Response to comment:
class A {
init?() {
return nil
}
func greet() {
print("hello")
}
}
let x = A()
if let x = x {
x.greet()
}
else {
print("nice try") //=> nice try
}
i tried this, hope this is what you want
private class My {
static var singletonObj = My()
}
let obj = My() // error
let obj1 = My.singletonObj
I would like to hide my Realm implementation and instead of working on RLMNotificationBlock I would like to use RXSwift. Below how my method looks like now (RLMNotificationBlock is a block that takes String and RLMRealm):
func addNotificationBlock(block: RLMNotificationBlock) -> RLMNotificationToken? {
let rlmObject = ...
return rlmObject.addNotificationBlock(block)
}
But I would like to switch to more reactive observer-pattern way. I looked at RxSwift docs and source code of rx_clickedButtonAtIndex, but I cannot figure out how I should put all these things together. I guess my code at the end would look like:
public var rx_realmContentChanged: ControlEvent<Int> {
let controlEvent = ControlEvent()
// My code go here
return controlEvent
}
I'm new with RXSwift and know only the basics. Any help will be appreciated.
There is an Rx Realm extension available on GitHub you can use: https://github.com/RxSwiftCommunity/RxRealm
It allows you to get an Observable out of a single Realm object or a Realm Collection. Here's an example from the README:
let realm = try! Realm()
let laps = realm.objects(Lap.self))
Observable.changesetFrom(laps)
.subscribe(onNext: { results, changes in
if let changes = changes {
// it's an update
print(results)
print("deleted: \(changes.deleted) inserted: \(changes.inserted) updated: \(changes.updated)")
} else {
// it's the initial data
print(results)
}
})
There is also an additional library especially built for binding table and collection views called RxRealmDataSources
If I understood you correctly, you just want to return Observable<RLMNotificationToken>
In this case you just need to do something like this
func addNotificationBlock(block: RLMNotificationBlock) -> Observable<RLMNotificationToken> {
return create { observer -> Disposable in
let rlmObject = ...
let token = rlmObject.addNotificationBlock(block)
// Some condition
observer.onNext(token)
// Some other condition
observer.onError(NSError(domain: "My domain", code: -1, userInfo: nil))
return AnonymousDisposable {
// Dispose resources here
}
// If you have nothing to dipose return `NopDisposable.instance`
}
}
In order to use it bind it to button rx_tap or other use flatMap operator
Apple has a nice explanation of Type (Class) Methods, however, their example looks like this:
class SomeClass {
class func someTypeMethod() {
// type method implementation goes here
}
}
SomeClass.typeMethod()
I see this exact same example parroted everywhere. My problem is, I need to call my Type Method from within an instance of my class, and that doesn't seem to compute.
I MUST be doing something wrong, but I noticed that Apple does not yet support Class Properties. I'm wondering if I'm wasting my time.
I tried this in a playground:
class ClassA
{
class func staticMethod() -> String { return "STATIC" }
func dynamicMethod() -> String { return "DYNAMIC" }
func callBoth() -> ( dynamicRet:String, staticRet:String )
{
var dynamicRet:String = self.dynamicMethod()
var staticRet:String = ""
// staticRet = self.class.staticMethod() // Nope
// staticRet = class.staticMethod() // No way, Jose
// staticRet = ClassA.staticMethod(self) // Uh-uh
// staticRet = ClassA.staticMethod(ClassA()) // Nah
// staticRet = self.staticMethod() // You is lame
// staticRet = .staticMethod() // You're kidding, right?
// staticRet = this.staticMethod() // What, are you making this crap up?
// staticRet = staticMethod() // FAIL
return ( dynamicRet:dynamicRet, staticRet:staticRet )
}
}
let instance:ClassA = ClassA()
let test:( dynamicRet:String, staticRet:String ) = instance.callBoth()
Does anyone have a clue for me?
var staticRet:String = ClassA.staticMethod()
This works. It doesn't take any parameters so you don't need to pass in any. You can also get ClassA dynamically like this:
Swift 2
var staticRet:String = self.dynamicType.staticMethod()
Swift 3
var staticRet:String = type(of:self).staticMethod()
In Swift 3 you can use:
let me = type(of: self)
me.staticMethod()
I wanted to add one more twist to this old question.
In today's Swift (I think it switched at 4, but I am not sure), we have replaced type(of: self) with Self (Note the capital "S").