Parse class objects no longer working in Xcode 8.3 - swift3

I recently updated my Xcode to Xcode 8.3.2 and ran the swift syntax updater. As a result of this update my Parse classes no longer work and I am not sure how fix them.
The error I am receiving is 'Static member 'registerSubclass' cannot be used on instance of type 'County'
The class looks like this.
import Foundation
class County : PFObject, PFSubclassing {
private lazy var __once: () = {
self.registerSubclass()
}()
#NSManaged var Name: String
override class func initialize() {
var onceToken : Int = 0;
_ = self.__once
}
class func parseClassName() -> String{
return "County"
}
}
I am also not understanding why in this update I am getting errors on when using self to call class level methods from within an enclosure?

Related

Cannot convert call result type 'Set<String>' to expected type 'String' error

I am working on an old Swift 3 project and I'm getting this error after updating it to Swift 4.2. It seems to work fine in Swift 3. I had to declare let NSURLPboardType = NSPasteboard.PasteboardType(kUTTypeURL as String) because NSURLPboardType does not exist in Swift 4.2 but otherwise the code is the same.
enum SparkleDrag {
static let type = "com.razeware.StickerDrag.AppAction"
static let action = "make sparkles"
}
let NSURLPboardType = NSPasteboard.PasteboardType(kUTTypeURL as String)
var nonURLTYpes: Set<String> {return [String(kUTTypeTIFF), SparkleDrag.type]}
var acceptableTypes: Set<String> {return [nonURLTYpes.union(NSURLPboardType)]}
The "u" in union is underlined with the error but I don't quite understand the nature of the problem. Any guidance would be much appreciated.
The problem is that NSURLPboardType is not a Set<String>, so the union cannot work.
If you're trying to get something like this:
["com.razeware.StickerDrag.AppAction", "public.url", "public.tiff"]
in aceptableTypes, you can simply forgo NSURLPboardType and do this:
enum SparkleDrag {
static let type = "com.razeware.StickerDrag.AppAction"
static let action = "make sparkles"
}
// let NSURLPboardType = NSPasteboard.PasteboardType(kUTTypeURL as String)
var nonURLTYpes: Set<String> {return [String(kUTTypeTIFF), SparkleDrag.type]}
var acceptableTypes: Set<String> {return nonURLTYpes.union([kUTTypeURL as String])}

Swift: reflection thanks to infoDictionary

I try to instanciate a new class from a string class name, like we can easy do in Java. I have finally wrote this function:
func stringClassFromString(_ className: String) -> AnyClass! {
let namespace = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String;
let cls: AnyClass = NSClassFromString("\(namespace).\(className)")!;
return cls;
}
Thanks to some googling, but as soon as I tried this solution thanks to a unit test :
func test() {
let myclass = stringClassFromString("NSDate") as! NSDate.Type
let instance = myclass.init()
print(instance)
}
I have an exception (Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)) as soon as my test use the line : let namespace... I tried to see the content of the infoDictionary, he's empty. So my question is simple, is it because of the unit tests context that my dictionary is empty ? Is there any method or library (like robolectric for android) to simulate an infoDicionary for testing purpose.
I succeed to find my error, instead of using the main Bundle, I use the constructor of the Bundle class to build a Bundle which will correspond to the current context :
let namespace = Bundle(for: type(of: self)).infoDictionary!["CFBundleExecutable"] as! String;
And tadda, it's working !

How to mock NSDate in Swift?

I have to test some date calculation but to do so I need to mock NSDate() in Swift. Whole app is written in Swift and I'd like to write test in it as well.
I've tried method swizzling but it doesn't work (or I'm doing something wrong which is more likely).
extension NSDate {
func dateStub() -> NSDate {
println("swizzzzzle")
return NSDate(timeIntervalSince1970: 1429886412) // 24/04/2015 14:40:12
}
}
test:
func testCase() {
let original = class_getInstanceMethod(NSDate.self.dynamicType, "init")
let swizzled = class_getInstanceMethod(NSDate.self.dynamicType, "dateStub")
method_exchangeImplementations(original, swizzled)
let date = NSDate()
// ...
}
but date is always current date.
Disclaimer -- I'm new to Swift testing so this may be a horribly hacky solution, but I've been struggling with this, too, so hopefully this will help someone out.
I found this explanation to be a huge help.
I had to create a buffer class between NSDate and my code:
class DateHandler {
func currentDate() -> NSDate! {
return NSDate()
}
}
then used the buffer class in any code that used NSDate(), providing the default DateHandler() as an optional argument.
class UsesADate {
func fiveSecsFromNow(dateHandler: DateHandler = DateHandler()) -> NSDate! {
return dateHandler.currentDate().dateByAddingTimeInterval(5)
}
}
Then in the test create a mock that inherits from the original DateHandler(), and "inject" that into the code to be tested:
class programModelTests: XCTestCase {
override func setUp() {
super.setUp()
class MockDateHandler:DateHandler {
var mockedDate:NSDate! = // whatever date you want to mock
override func currentDate() -> NSDate! {
return mockedDate
}
}
}
override func tearDown() {
super.tearDown()
}
func testAddFiveSeconds() {
let mockDateHandler = MockDateHandler()
let newUsesADate = UsesADate()
let resultToTest = usesADate.fiveSecondsFromNow(dateHandler: mockDateHandler)
XCTAssertEqual(resultToTest, etc...)
}
}
If you want to swizzle it you need to swizzle a class that is internally used by NSDate and it is __NSPlaceholderDate. Use this only for testing since it is a private API.
func timeTravel(to date: NSDate, block: () -> Void) {
let customDateBlock: #convention(block) (AnyObject) -> NSDate = { _ in date }
let implementation = imp_implementationWithBlock(unsafeBitCast(customDateBlock, AnyObject.self))
let method = class_getInstanceMethod(NSClassFromString("__NSPlaceholderDate"), #selector(NSObject.init))
let oldImplementation = method_getImplementation(method)
method_setImplementation(method, implementation)
block()
method_setImplementation(method, oldImplementation)
}
And later you can use like this:
let date = NSDate(timeIntervalSince1970: 946684800) // 2000-01-01
timeTravel(to: date) {
print(NSDate()) // 2000-01-01
}
As others suggested I would rather recommend introducing a class Clock or similar that you can pass around and get a date from it and you can easily replace it with an alternative implementation in your tests.
Rather than use swizzling you should really design your system to support testing. If you do a lot of data processing then you should inject the appropriate date into the functions which use it. In this way your test injects the dates into these functions to test them and you have other tests which verify that the correct dates will be injected (when you stub the methods that use the dates) for various other situations.
Specifically for your swizzling problem, IIRC NSDate is a class cluster so the method you're replacing is unlikely to be called as a different class will be 'silently' created and returned.

How to retrieve a list of all classes in a module in Haxe? (aka: helper classes)

Considering a Haxe file defines a series of classes like so:
import flash.display.MovieClip;
import flash.display.Sprite;
import haxe.unit.TestCase;
class MainTest extends Sprite {
public var testcase:Array<Class<TestCase>> = ???;
}
class TestSprite extends TestCase {
function testBasic() {
var sprite = new Sprite();
sprite.x = 0;
assertEquals(sprite.x, 0);
}
}
class TestMovieClip extends TestCase {
function testMovieClip() {
var mc = new MovieClip();
mc.nextFrame();
assertEquals(mc.currentFrame, 2);
}
}
Is there a way to obtain a list of all the helper classes (ex: TestSprite and TestMovieClip)? Preferably at runtime, but a macro that would return an Array<Class<TestCase>> would work fine too.
I have a small macro helper library called compiletime that can get all classes in a package, or all classes that extend a certain class.
haxelib install compiletime
And then get classes either by base class or by package:
var testcases = CompileTime.getAllClasses("my.package");
var testcases = CompileTime.getAllClasses(TestCase);
var testcases = CompileTime.getAllClasses("my.package",TestCase); // Both!
Now, that is getting them by package, not by module. Getting it by module might work, I'm not sure off the top of my head. But if you were to edit this part of the code:
https://github.com/jasononeil/compiletime/blob/master/src/CompileTime.hx#L208
And change it to also support getting by module, and send me a pull request, I would most certainly merge it :)
Good luck!

Derived Class Method of Generic Class Template not being called

I have a generic class for making and processing JSON API requests. I pass in the TParam and TResult template parameters but when I use a derived type it's implementation is not being called.
Here is some code you can throw in a playground to illustrate:
import Cocoa
// Base class for parameters to POST to service
class APIParams {
func getData() -> Dictionary<String, AnyObject> {
return Dictionary<String, AnyObject>()
}
}
// Base class for parsing a JSON Response
class APIResult {
func parseData(data: AnyObject?) {
}
}
// Derived example for a login service
class DerivedAPIParams: APIParams {
var user = "some#one.com"
var pass = "secret"
// THIS METHOD IS CALLED CORRECTLY
override func getData() -> Dictionary<String, AnyObject> {
return [ "user": user, "pass": pass ]
}
}
// Derived example for parsing a login response
class DerivedAPIResult: APIResult {
var success = false
var token:String? = ""
// THIS METHOD IS NEVER CALLED
override func parseData(data: AnyObject?) {
/*
self.success = data!.valueForKey("success") as Bool
self.token = data!.valueForKey("token") as? String
*/
self.success = true
self.token = "1234"
}
}
class APIOperation<TParams: APIParams, TResult: APIResult> {
var url = "http://localhost:3000"
func request(params: TParams, done: (NSError?, TResult?) -> ()) {
let paramData = params.getData()
// ... snip making a request to website ...
let result = self.parseResult(nil)
done(nil, result)
}
func parseResult(data: AnyObject?) -> TResult {
var result = TResult.self()
// This should call the derived implementation if passed, right?
result.parseData(data)
return result
}
}
let derivedOp = APIOperation<DerivedAPIParams, DerivedAPIResult>()
let params = DerivedAPIParams()
derivedOp.request(params) {(error, result) in
if result? {
result!.success
}
}
The really weird thing is that only the DerivedAPIResult.parseData() is not called, whereas the DerivedAPIParams.getData() method is called. Any ideas why?
UPDATE: This defect is fixed with XCode 6.3 beta1 (Apple Swift version 1.2 (swiftlang-602.0.37.3 clang-602.0.37))
Added info for a workaround when using XCode 6.1 (Swift 1.1)
See these dev forum threads for details:
https://devforums.apple.com/thread/251920?tstart=30
https://devforums.apple.com/message/1058033#1058033
In a very similar code sample I was having the exact same issue. After waiting through beta after beta for a "fix", I did more digging and discovered that I can get the expect results by making the base class init() required.
By way of example, here is Matt Gibson's reduced example "fixed" by adding the proper init() to ApiResult
// Base class for parsing a JSON Response
class APIResult {
// adding required init() to base class yields the expected behavior
required init() {}
}
// Derived example for parsing a login response
class DerivedAPIResult: APIResult {
}
class APIOperation<TResult: APIResult> {
init() {
// EDIT: workaround for xcode 6.1, tricking the compiler to do what we want here
let tResultClass : TResult.Type = TResult.self
var test = tResultClass()
// should be able to just do, but it is broken and acknowledged as such by Apple
// var test = TResult()
println(test.self) // now shows that we get DerivedAPIResult
}
}
// Templated creation creates APIResult
let derivedOp = APIOperation<DerivedAPIResult>()
I do not know why this works. If I get time I will dig deeper, but my best guess is that for some reason having required init is causing different object allocation/construction code to be generated that forces proper set up of the vtable we are hoping for.
Looks possibly surprising, certainly. I've reduced your case to something rather simpler, which might help to figure out what's going on:
// Base class for parsing a JSON Response
class APIResult {
}
// Derived example for parsing a login response
class DerivedAPIResult: APIResult {
}
class APIOperation<TResult: APIResult> {
init() {
var test = TResult()
println(test.self) // Shows that we get APIResult, not DerivedAPIResult
}
}
// Templated creation creates APIResult
let derivedOp = APIOperation<DerivedAPIResult>()
...so it seems that creating a new instance of a templated class with a type constraint gives you an instance of the constraint class, rather than the derived class you use to instantiate the specific template instance.
Now, I'd say that the generics in Swift, looking through the Swift book, would probably prefer you not to create your own instances of derived template constraint classes within the template code, but instead just define places to hold instances that are then passed in. By which I mean that this works:
// Base class for parsing a JSON Response
class APIResult {
}
// Derived example for parsing a login response
class DerivedAPIResult: APIResult {
}
class APIOperation<T: APIResult> {
var instance: T
init(instance: T) {
self.instance = instance
println(instance.self) // As you'd expect, this is a DerivedAPIResult
}
}
let derivedOpWithPassedInstance = APIOperation<DerivedAPIResult>(instance: DerivedAPIResult())
...but I'm not clear whether what you're trying should technically be allowed or not.
My guess is that the way generics are implemented means that there's not enough type information when creating the template to create objects of the derived type from "nothing" within the template—so you'd have to create them in your code, which knows about the derived type it wants to use, and pass them in, to be held by templated constrained types.
parseData needs to be defined as a class func which creates an instance of itself, assigns whatever instance properties, and then returns that instance. Basically, it needs to be a factory method. Calling .self() on the type is just accessing the type as a value, not an instance. I'm surprised you don't get some kind of error calling an instance method on a type.