Skip to content

Commit

Permalink
Merge pull request #59 from Dimillian/master
Browse files Browse the repository at this point in the history
Support multiple bundle identifiers
  • Loading branch information
evermeer committed Mar 18, 2016
2 parents 20db5d7 + eb8176a commit 2e0a11b
Showing 1 changed file with 44 additions and 6 deletions.
50 changes: 44 additions & 6 deletions EVReflection/pod/EVReflection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,9 @@ final public class EVReflection {
/// Variable that can be set using setBundleIdentifier
private static var bundleIdentifier:String? = nil

/// Variable that can be set using setBundleIdentifiers
private static var bundleIdentifiers:[String]? = nil

/**
This method can be used in unit tests to force the bundle where classes can be found

Expand All @@ -378,15 +381,36 @@ final public class EVReflection {
*/
public class func setBundleIdentifier(forClass: AnyClass) {
if let bundle:NSBundle = NSBundle(forClass:forClass) {
let appName = (bundle.infoDictionary![kCFBundleNameKey as String] as! String).characters.split(isSeparator: {$0 == "."}).map({ String($0) }).last ?? ""
//let appName = (bundle.bundleIdentifier!).characters.split(isSeparator: {$0 == "."}).map({ String($0) }).last ?? ""
let cleanAppName = appName
.stringByReplacingOccurrencesOfString(" ", withString: "_", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)
.stringByReplacingOccurrencesOfString("-", withString: "_", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)
EVReflection.bundleIdentifier = cleanAppName
EVReflection.bundleIdentifier = bundleForClass(forClass, bundle: bundle)
}
}

/**
This method can be used in project where models are split between multiple modules.

:param: classes classes that that will be used to find the appName for in which we can find classes by string.

:returns: Nothing
*/
public class func setBundleIdentifiers(classes: Array<AnyClass>) {
bundleIdentifiers = []
for aClass in classes {
if let bundle:NSBundle = NSBundle(forClass:aClass) {
bundleIdentifiers?.append(bundleForClass(aClass, bundle: bundle))
}
}
}

private static func bundleForClass(forClass: AnyClass, bundle: NSBundle) -> String {
let appName = (bundle.infoDictionary![kCFBundleNameKey as String] as! String).characters.split(isSeparator: {$0 == "."}).map({ String($0) }).last ?? ""
//let appName = (bundle.bundleIdentifier!).characters.split(isSeparator: {$0 == "."}).map({ String($0) }).last ?? ""
let cleanAppName = appName
.stringByReplacingOccurrencesOfString(" ", withString: "_", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)
.stringByReplacingOccurrencesOfString("-", withString: "_", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)
return cleanAppName
}


/// This dateformatter will be used when a conversion from string to NSDate is required
private static var dateFormatter: NSDateFormatter? = nil

Expand Down Expand Up @@ -434,6 +458,20 @@ final public class EVReflection {
let appName = getCleanAppName()
classStringName = "\(appName).\(className)"
}

if let classStringName = NSClassFromString(classStringName) {
return classStringName
}

if let bundleIdentifiers = bundleIdentifiers {
for aBundle in bundleIdentifiers {
let className = "\(aBundle).\(className)"
if let existingClass = NSClassFromString(className) {
return existingClass
}
}
}

return NSClassFromString(classStringName)
}

Expand Down

0 comments on commit 2e0a11b

Please sign in to comment.