swift Tips(version1.2 xcode6.4)

1酷

颜色转图片

extension UIImage{
    static func ls_imageWithColor(color: UIColor) -> UIImage
    {
        let imageSize = CGSizeMake(1, 1);
        UIGraphicsBeginImageContextWithOptions(imageSize, true, 0);
        color.set();
        let path = UIBezierPath(rect: CGRectMake(0, 0, imageSize.width, imageSize.height))
        path.fill()
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
    
}

 2酷

方向

    // MARK: - Orientations
    override func shouldAutorotate() -> Bool {
        return true
    }
    
    override func supportedInterfaceOrientations() -> Int {
        return UIInterfaceOrientationMask.Portrait.rawValue.hashValue
    }

 3酷

 swift 调用oc枚举不识别

typedef NS_ENUM(NSInteger, EZCameraState)
{
    EZCameraStateFront,
    EZCameraStateBack
};

 改成:

typedef NS_ENUM(NSInteger, EZCameraState)
{
    Front,
    Back
};

 4酷

背景图片不要用(其实以前也是):

 

        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "videoAotu")!)//patternImage耗内存

用:

 

        self.view.layer.contents = UIImage(named: "videoAotu")?.CGImage

5酷

oc中的#pragma mark  在swift中是://MARK:    其他还有 //TODO:、//FIXME。

6酷

 在子类必须实现的方法中调用这个函数fatalError("方法未实现")。使用了fatalError方法的地方编译可过。但是在运行的时候会报错!

7酷

swift1.2中没有appearanceWhenContainedIn:方法 http://justsee.iteye.com/blog/2227009

 8酷

swift单例

class LSHostAppManager{
    
    class func shareInstance()->LSHostAppManager{
        struct LSSingleton{
            static var predicate:dispatch_once_t = 0
            static var instance:LSHostAppManager? = nil
        }
        dispatch_once(&LSSingleton.predicate,{
            LSSingleton.instance=LSHostAppManager()
            }
        )
        return LSSingleton.instance!
    }
}

 

struct StructSingleton{
    static func shareInstance()->StructSingleton{
        struct YRSingleton{
            static var predicate:dispatch_once_t = 0
            static var instance:StructSingleton? = nil
        }
        dispatch_once(&YRSingleton.predicate,{
                YRSingleton.instance=StructSingleton()
            }
        )
        return YRSingleton.instance!
    }
}

 http://www.cocoachina.com/industry/20140620/8894.html

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(version)