Showing posts with label Swift. Show all posts
Showing posts with label Swift. Show all posts

Tuesday, May 23, 2017

Swift: Reading from Standard Streams (標準ストリーム)

Command:

$ cat ./Desktop/StandardInput/StandardInput/main.swift


Result:

import Foundation

var string = ""

func showMainMenu(){
    print("1) Walk")
    print("2) Study")
    print("3) Work")
    print("4) Sleep")
    print("> ", terminator: "")
}

func walk(){
    var line = ""
   
    repeat {
        print("Walking...")
        print("Enter 'exit' to finish")
        print("> ", terminator: "")
        line = readLine()!
    } while line != "exit"
}

func work(){
    var line = ""
   
    repeat {
        print("Working...")
        print("Enter 'exit' to finish")
        print("> ", terminator: "")
        line = readLine()!
    } while line != "exit"
}

func study(){
    var line = ""
   
    repeat {
        print("Studying...")
        print("Enter 'exit' to finish")
        print("> ", terminator: "")
        line = readLine()!
    } while line != "exit"
}

func sleep(){
    var line = ""
   
    repeat {
        print("Sleeping...")
        print("Enter 'exit' to finish")
        print("> ", terminator: "")
        line = readLine()!
    } while line != "exit"
}

showMainMenu()

while let line = readLine(){
    switch line{
    case "1":
        walk()
        break
    case "2":
        study()
        break
    case "3":
        work()
        break
    case "4":
        sleep()
        break
    default:
        print("Enter again")
        break
    }
    showMainMenu()
}


Command:

$ ./Library/Developer/Xcode/DerivedData/StandardInput-eauwzvcnsumxppbvwnagppvlylcg/Build/Products/Debug/StandardInput ; exit;


Result:

1) Walk
2) Study
3) Work
4) Sleep
> 1
Walking...
Enter 'exit' to finish
>
Walking...
Enter 'exit' to finish
>
Walking...
Enter 'exit' to finish
> exit
1) Walk
2) Study
3) Work
4) Sleep
> 2
Studying...
Enter 'exit' to finish
>
Studying...
Enter 'exit' to finish
>
Studying...
Enter 'exit' to finish
>
Studying...
Enter 'exit' to finish
> exit
1) Walk
2) Study
3) Work
4) Sleep
> 3
Working...
Enter 'exit' to finish
> exit
1) Walk
2) Study
3) Work
4) Sleep
> 4
Sleeping...
Enter 'exit' to finish
>
Sleeping...
Enter 'exit' to finish
>
Sleeping...
Enter 'exit' to finish
>
Sleeping...
Enter 'exit' to finish
>
Sleeping...
Enter 'exit' to finish
> exit
1) Walk
2) Study
3) Work
4) Sleep
> exit
Enter again
1) Walk
2) Study
3) Work
4) Sleep
> ^C
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

[Process completed]

Swift: Command Line Argument Parsing

Command:

$ cat ./Desktop/Arguments\ Test/Arguments\ Test/main.swift


Result:

import Foundation

var count = 0

for argument in CommandLine.arguments {
    print("Arg[\(count)]: \(argument)")
    count += 1
}


Command:

$ ./Library/Developer/Xcode/DerivedData/Arguments_Test-ffnafkbrsdftjxdawcsyrdhlfnvu/Build/Products/Debug/Arguments\ Test first second third


Result:

Arg[0]: ./Library/Developer/Xcode/DerivedData/Arguments_Test-ffnafkbrsdftjxdawcsyrdhlfnvu/Build/Products/Debug/Arguments Test
Arg[1]: first
Arg[2]: second
Arg[3]: third

Thursday, May 4, 2017

Swift: Random Message Example

Command:

$ cat /Users/username/Desktop/macOS\ Swift\ Test/macOS\ Swift\ Test/main.swift


Result:

import Foundation
import Darwin

let goodmorning: Array<String> = ["おはよう!","おはようございます。","やあ!","ねむたそうだね","おっはー!","グッドモーニング!","モーニング!","今日もよろしく!"]

let randomIndex = Int(arc4random_uniform(UInt32(goodmorning.count)))

print("\(goodmorning[randomIndex])")


Command:

$ /Users/username/Library/Developer/Xcode/DerivedData/macOS_Swift_Test-arwavijyybnxpmctpfzeonufbnvy/Build/Products/Debug/macOS\ Swift\ Test ;

Result:

モーニング!

Wednesday, January 27, 2016

Swift: Adding Sample to Health Store

Swift
@IBAction func addPotassiumTapped(sender : AnyObject){

let potassiumType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryPotassium)

let potassiumQuantity = HKQuantity(unit: HKUnit.gramUnit(), doubleValue: 0.0055)

let potassiumSample = HKQuantitySample(type: potassiumType!, quantity: potassiumQuantity, startDate: NSDate(), endDate: NSDate())

let healthKitStore:HKHealthStore = HKHealthStore()

healthKitStore.saveObject(potassiumSample, withCompletion: { (success, error) -> Void in

if( error != nil ) {

print("Error saving Potassium sample: \(error!.localizedDescription)")

} else {

print("Potassium sample saved successfully!")

}
})
}

Tuesday, January 26, 2016

Swift: Request Authorization to Dietary Potassium Data

Swift

@IBOutlet weak var aButton: UIButton!

@IBAction func buttonTapped(sender : AnyObject){

let healthKitStore:HKHealthStore = HKHealthStore()

let healthKitTypesToWrite = Set(
arrayLiteral: HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryPotassium)! )

healthKitStore.requestAuthorizationToShareTypes(healthKitTypesToWrite, readTypes:nil){ (success, error) -> Void in
if success == false {
}
}

}

Swift: Display an alert

Swift
@IBOutlet weak var aButton: UIButton!

@IBAction func buttonTapped(sender : AnyObject){

let alertController = UIAlertController(title: "My alert", message:

"This is an alert!", preferredStyle: UIAlertControllerStyle.Alert)

alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))

self.presentViewController(alertController, animated: true, completion: nil)
}

Thursday, January 14, 2016

Swift: Dialog box

Initialization:
var alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)