1 頁 (共 1 頁)

擴充方法

發表於 : 週六 12月 05, 2015 3:43 pm
rusli

代碼: 選擇全部

import Foundation
import SQLite

extension UIImage
{
    func toBase64() -> String
    {
        let imageData = UIImagePNGRepresentation(self)!
        return imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
    }
}

extension UIViewController
{
    func ShowDismissMessage(title: String, message: String)
    {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))
        self.presentViewController(alertController, animated: true, completion: nil)
       
        alertController.AutoDismiss()
    }
   
    func ShowCompletedMessage(title: String, message: String)
    {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.Default,handler: nil))
        self.presentViewController(alertController, animated: true, completion: nil)
       
        alertController.AutoDismiss()
    }
   
    func ShowConfirmMessage(message: String, okAction: () -> Void, cancelAction: () -> Void)
    {
        let alertController = UIAlertController(title: "Confirm", message: message, preferredStyle: UIAlertControllerStyle.Alert)
 
        alertController.addAction(UIAlertAction(title: "Do it!", style: .Default, handler: { (action: UIAlertAction!) in
            okAction()
        }))
       
        alertController.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: { (action: UIAlertAction!) in
            cancelAction()
        }))
       
        self.presentViewController(alertController, animated: true, completion: nil)
    }
}

extension UIAlertController
{
    func AutoDismiss()
    {
        // Delay the dismissal by 2 seconds
        let delay = 2.0 * Double(NSEC_PER_SEC)
        let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
        dispatch_after(time, dispatch_get_main_queue(), {
            self.dismissViewControllerAnimated(true, completion: nil)
        })
    }
}

extension String
{
    func AddTenPercent() -> String
    {
        if(GlobalVariables.IsCanada)
        {
            return String(Float(self)! + Float(self)! * 0.1)
        }
       
        return self
    }
   
    func ShowCanadaOrOriginal(canadaMSRP: String) -> String
    {
        if(GlobalVariables.IsCanada)
        {
            return canadaMSRP
        }
       
        return self
    }
   
    func ShowCanadaOrOriginal(style: String, model: String) -> String
    {
        if(GlobalVariables.IsCanada)
        {
            print("IsCanada: \(style)  \(model)")
            let m = ProductModel()
            let vm: [ProductViewModel] = m.GetAllByStyleAndModel(style, model: model)
            print("\(vm[0].CanadaMSRP)")
            return vm.count > 0 ? vm[0].CanadaMSRP : "N/A"
        }
        print("Not IsCanada: \(style)  \(model)")

        return self
    }
   
    func toDouble() -> Double? {
        return NSNumberFormatter().numberFromString(self)?.doubleValue
    }
   
    var floatValue: Float {
        return (self as NSString).floatValue
    }
   
    func toImage() -> UIImage
    {
        let decodeData = NSData(base64EncodedString: self, options: NSDataBase64DecodingOptions(rawValue: 0))
        let decodedIamge = UIImage(data: decodeData!)
        return decodedIamge!
    }
   
    func replace(target: String, withString: String) -> String
    {
        return self.stringByReplacingOccurrencesOfString(target, withString: withString, options: NSStringCompareOptions.LiteralSearch, range: nil)
    }
   
    func toCurrency() -> String
    {
        let formatter = NSNumberFormatter()
        formatter.numberStyle = .CurrencyStyle
        return formatter.stringFromNumber(Float(self)!)!
    }

    func PadLeft(totalWidth: Int, withString: String) -> String {
        let toPad = totalWidth - self.characters.count
        if toPad < 1 {
            return self
        }
       
        return "".stringByPaddingToLength(toPad, withString: withString, startingAtIndex: 0) + self
    }
   
    func PadLeft(totalWidth: Int) -> String {
        return self.PadLeft(totalWidth, withString: " ")
    }
   
    func PadRight(totalWidth: Int) -> String {
        return self.PadRight(totalWidth, withString: " ")
    }
   
    func PadRight(totalWidth: Int, withString: String) -> String {
        return stringByPaddingToLength(totalWidth, withString: withString, startingAtIndex: 0)
    }
   
    static func IsNullOrEmpty(value: String?) -> Bool
    {
        return value == nil || value!.isEmpty
    }
   
    static func IsNullOrWhiteSpace(value: String?) -> Bool
    {
        return IsNullOrEmpty(value) ||
            (value!.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())).characters.count == 0
    }
}

extension Float
{
    func toCurrency() -> String
    {
        let formatter = NSNumberFormatter()
        formatter.numberStyle = .CurrencyStyle
        return formatter.stringFromNumber(self)!
    }

    func toInt() -> Int? {
        if self > Float(Int.min) && self < Float(Int.max) {
            return Int(self)
        } else {
            return nil
        }
    }
}

// 擴充到 NSDate 物件
extension NSDate
{
    func FullDateTime() -> String
    {
         return xxx 
    }   
}