Hi!
Sorry for the incosinstency of my posts. I'll try to be more specific now:
I'l try to explain what the problem is. I'll attach the full source at the end of this post.
1. I use Xcode Version 8.3.3 (8E3004b), so I'm using Swift 3
2. I've created a Swift-Framework (teststats) project targeting macOS with two classes so far: Examine and StatsError
3. Both classes defined as "public"
4. To test the functionality of the framework I've created a second target within the same Xcode project (command line tool written in swift).
5. The command line tool is linked against the framework.
The class examine is defined as a "generic" class. It is necessary to check the actual type of the used objects within the class Examine at runtime.
The if-statement in "init" evaluates always to "false" when running the command line tool. When the same expression is evaluated via "po object is Array<Element>" in the debugger (after setting a breakpoint), the result is "true".
Sourcecode:
1. main.swift (command line tool target)
//
// main.swift
// TestExamine
//
import Foundation
import teststats
var test: Examine<Double>
do {
test = try Examine<Double>(withObject: [3.14,2.23, 0.575], characterSet: NSCharacterSet.alphanumerics as NSCharacterSet)
print(test.descriptionString!)
}
catch {
let e = error as! StatsError
print(e.type)
}
2. Framework target
2.1. Examine.swift
//
// Examine.swift
// teststats
//
import Foundation
import os.log
public class Examine<Element: Hashable>: NSCopying, NSCoding {
/// A user defined tag to identify the instance
public var tag: Any?
/// A human readable description
public var descriptionString: String?
public init() {
initialize()
}
/// Initializes a Examine instance using a string or an array<Element>
/// Desginated Initializer
public init(withObject object: Any, characterSet: NSCharacterSet?) throws {
// allow only arrays as 'object'
if (object is Array<Element>) {
self.initialize(withArray: object as! NSArray)
}
else {
os_log("Error creating Examine instance", log: log_stat, type: .error)
throw StatsError(type: .invalidArgument, file: #file, line: #line, function: #function)
}
}
private func initialize(withString string: String, characterSet: NSCharacterSet) {
initialize()
return
}
private func initialize(withArray array: NSArray) {
initialize()
return
}
public func encode(with aCoder: NSCoder) {
aCoder.encode(42, forKey: "magic")
}
required public init?(coder aDecoder: NSCoder) {
}
public func copy(with zone: NSZone? = nil) -> Any {
let res: Examine = Examine()
return res
}
fileprivate func initialize() {
descriptionString = "Description"
tag = "";
}
fileprivate func elementIsType<T>(object: Any!, type: T.Type) -> Bool {
return object is T
}
}
2.2. StatsError.swift
//
// StatsError.swift
//
import Foundation
import os.log
let log_stat = OSLog(subsystem: "local.examine.stats", category: "Examine")
/// Custom error class
public class StatsError: NSError, LocalizedError {
public var type: ErrorType = .none
public var line: Int = 0
public var function: String = ""
public var file: String = ""
/// Error codes
public enum ErrorType: Int {
/// No error
case none
/// Invalid argments
case invalidArgument
}
/// A string describing the error
override public var localizedDescription: String {
return "Error description"
}
/// A string describing the reason for failure
override public var localizedFailureReason: String? {
return "A Reason"
}
/// Init
public init(type: ErrorType, file: String, line: Int, function: String) {
super.init(domain: "local.examine.stats", code: type.rawValue, userInfo: nil)
self.type = type
self.line = line
self.function = function
self.file = file
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}