ClassyKit / Classy

Expressive, flexible, and powerful stylesheets for UIView and friends.
http://classykit.github.io/Classy/
MIT License
740 stars 77 forks source link

Swift Live Reload #94

Closed tobinharris closed 6 years ago

tobinharris commented 9 years ago

Would be great to see an example of how to get Live Reload working with Swift. I can't get CASAbsoluteFilePath to be available despite adding to the bridging header.

keithnorm commented 9 years ago

I've found it helpful to write the watcher initializer in Obj-C still in order to take advantage of the #if TARGET_IPHONE_SIMULATOR preprocessor. I am doing it through an extension of CASStyler, but if doesn't have to be. e.g.:

// CASStyle+Additions.h
#import <Classy/Classy.h>

@interface CASStyler(Additions)

+ (void)initWatcher;

@end
// CASStyler+Additions.m

#import "CASStyler+Additions.h"

@implementation CASStyler (Additions)

+ (void)initWatcher {
#if TARGET_IPHONE_SIMULATOR
    NSString *absoluteFilePath = CASAbsoluteFilePath(@"../Resources/stylesheets/stylesheet.cas");
    [CASStyler defaultStyler].watchFilePath = absoluteFilePath;
#endif
}

@end
// AppDelegate.swift

import Classy

public class AppDelegate: UIResponder, UIApplicationDelegate {
   public func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        CASStyler.initWatcher()
  }
}
greymouser commented 9 years ago

Another option besides using the bridging header and the macro directly is just to use Swift and the FILE macro:

        if UIDevice.currentDevice().systemName.compare("iPhone Simulator") == .OrderedSame {
            let curFilePath = __FILE__
            let curFileDir = curFilePath.stringByDeletingLastPathComponent;
            let styleFilePath = curFileDir.stringByAppendingPathComponent("StyleSheets/stylesheet.cas")
            CASStyler.defaultStyler().watchFilePath = styleFilePath
        }
keithnorm commented 9 years ago

Very nice, thanks!

dnedrow commented 6 years ago

Added to wiki.