Skip to content

Commit

Permalink
Minor optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
arturdev committed Apr 10, 2020
1 parent 824ca5d commit 650e760
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Unrealm.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Pod::Spec.new do |s|
s.name = 'Unrealm'
s.swift_version = '5.1'
s.version = '1.2.0'
s.version = '1.2.1'
s.summary = 'Unrealm is an extension on RealmCocoa, which enables Swift native types to be saved in Realm.'
s.description = <<-DESC
Unrealm enables you to easily store Swift native Classes, Structs and Enums into Realm.
Expand Down
8 changes: 8 additions & 0 deletions Unrealm/Classes/ObjC/Object+Unrealm.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@ void prepareUnrealm(void)
{
[RealmSwiftObject prepareUnrealm];
}

@implementation NSObject(TypeString)
- (NSString *)typeString
{
NSString *str = NSStringFromClass([self class]);
return str;
}
@end
4 changes: 4 additions & 0 deletions Unrealm/Classes/ObjC/include/Object+Unrealm.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@

void prepareUnrealm(void);

@interface NSObject(TypeString)
- (NSString *)typeString;
@end

#endif /* Object_Unrealm_h */
14 changes: 2 additions & 12 deletions Unrealm/Classes/Swift/Unrealm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -371,25 +371,15 @@ public extension Object {
}

func toRealmable() -> RealmableBase? {
let objTypeString = self.typeStr()
if let type = objectsAndRealmables[objTypeString] {
let objTypeString = self.typeString() ?? ""
if let type = objectsAndRealmables.first(where: {objTypeString.contains($0.key)})?.value {
let convertedObj = self.toRealmable(of: type)
return convertedObj as? RealmableBase
}
return nil
}
}

extension Object {
func typeStr() -> String {
let t = String(describing: self)
if let match = t.range(of: "[a-zA-Z0-9_.]+", options: .regularExpression) {
return String(t[match])
}
return ""
}
}

fileprivate func convert<T: NSObject>(val: Any, to objectType: T.Type) -> AnyObject? {
let mirror = Mirror(reflecting: val)
let children = mirror.childrenIncludingSuperclass(subject: val)
Expand Down

0 comments on commit 650e760

Please sign in to comment.