Swift 入門指南 V1.00 - ViewController.swift




ViewController.swift


//
//  ViewController.swift
//  EncryptorMacOS
//
//  Created by 張凱慶 on 2015/3/4.
//  Copyright (c) 2015年 張凱慶. All rights reserved.
//

import Cocoa

class ViewController: NSViewController {
    
    @IBOutlet weak var display: NSTextField!
    @IBOutlet weak var input: NSTextField!
    @IBOutlet weak var output: NSTextField!
    
    var e: Encrypt?
    var inputText = ""
    var outputText = ""
    let filename = "encryptor"
    
    @IBAction func newMethod(sender: AnyObject) {
        e = Encrypt()
        
        if e != nil {
            display.stringValue = e!.code.description
        }
    }
    
    @IBAction func loadMethod(sender: AnyObject) {
        var isFileExist = NSFileManager.defaultManager().fileExistsAtPath(filename)
        if isFileExist {
            e = NSKeyedUnarchiver.unarchiveObjectWithFile(filename) as? Encrypt
            display.stringValue = "Encrypt object is loaded."
        }
        else {
            display.stringValue = "Encrypt object is not loaded."
        }
    }
    
    @IBAction func saveMethod(sender: AnyObject) {
        var succeed = NSKeyedArchiver.archiveRootObject(e!, toFile: filename)
        if succeed {
            display.stringValue = "Encrypt object is saved."
        }
        else {
            display.stringValue = "Encrypt object is not saved."
        }
    }
    
    @IBAction func encodeMethod(sender: AnyObject) {
        inputText = input.stringValue
        
        if inputText == "" {
            display.stringValue = "Please input something!!"
        }
        else {
            if e != nil {
                outputText = e!.toEncode(inputText)
                output.stringValue = outputText
                
                display.stringValue = "The result is above."
            }
            else {
                display.stringValue = "No Encrypt object!!"
            }
        }
    }
    
    @IBAction func decodeMethod(sender: AnyObject) {
        inputText = input.stringValue
        
        if inputText == "" {
            display.stringValue = "Please input something!!"
        }
        else {
            if e != nil {
                outputText = e!.toDecode(inputText)
                output.stringValue = outputText
                
                display.stringValue = "The result is above."
            }
            else {
                display.stringValue = "No Encrypt object!!"
            }
        }
    }
    
    @IBAction func copyMethod(sender: AnyObject) {
        var pasteBoard = NSPasteboard.generalPasteboard()
        pasteBoard.clearContents()
        pasteBoard.writeObjects([outputText])
        
        display.stringValue = "The result is copied to clipboard."
    }
    
    @IBAction func clearMethod(sender: AnyObject) {
        input.stringValue = ""
        output.stringValue = ""
        e = nil
        inputText = ""
        outputText = ""
        
        display.stringValue = "It is all clear."
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override var representedObject: AnyObject? {
        didSet {
        // Update the view, if already loaded.
        }
    }


}

the end

沒有留言: