您的位置:首页 > 移动开发 > Swift

IOS:Swift基本语法

2016-03-01 10:23 429 查看
//
//  main.swift
//  SwiftLeanOSX
//
//  Created by intbird on 15/10/31.
//  Copyright © 2015年 intbird. All rights reserved.
//

import Foundation

//////////////////////////////////////////简单值

typealias javaInt = Int;
var javaNumber = javaInt.min;
print(javaNumber)

var myVariable = 42;
let myConstant = 42;

let label = "this width is ";
let width = 94;
let widthLabel = label + String(width) + ".";
print(widthLabel);

let apples = 3;
let oranges = 5;
let appSummary = "i have \(apples) apples";
let fruitSummary = "I have \(apples + oranges) fruits";
print(fruitSummary);

//////////////////////////////////////////数组

var shoppingList = ["catfish","water","tulips","blue paint"];
shoppingList[1] = " bottle of water";
var occupations = [
"Malcolm":"Captain",
"Kaylee":"Mechanic"]
occupations["Jayne"]="Public Relations";

var emptyArray = [Int]();
let emptyDictionary = Dictionary<String,Float>();

let stringArray = ["1","2"];
var doubleValue = [Double](count: 3, repeatedValue: 0.0);
var floatValue = Array(count: 4, repeatedValue: 0.0);

/////////////// 判断

let scores = [12,44,63,52,32,103,88,95,56];
var teamScore = 0;
for score in scores{
if score > 50 {
teamScore += 3;
}else{
teamScore += 1;
}
}
print(teamScore);

var optionalName :String? = "intbird";
//optionalName = nil;
var greeting = "Hello! "
if let name = optionalName {
greeting = "Hello,\(name)";
}
print(greeting);

//////////////////////////////////////////switch

let vegetable = "red pepper";
switch vegetable{
case "calery":
let vegetableComment = "hello intbird";
break;
case "cucumber","catercress":
break;
case let x where x.hasSuffix("pepper"):
break;
default:
break;
}

//////////////////////////////////////////for 循环

let interestingNumbers = [
"Prime":[1,2,3,4,5],
"fibonacci":[1,1,2,3,5,8],
"square":[1,4,9,16,25]
]
var largest = 10;
for (kind,numbers) in interestingNumbers{
for number in numbers{
if number > largest{
print(number);
}
}
}

var firstForLoop = 0;
for i in 0...3 {
firstForLoop += i;
}
print(firstForLoop)

//////////////////////////////////////////while 循环

var n = 2
while n < 100{
n *= 2;
}

var m = 2;
repeat {
m = m * 2;
}while m < 100;

//////////////////////////////////////////函数和闭包

//a...b 完全包括a,b;
//a..b 不包含b;

func greet(name:String, day:String)->String{
return "Hello \(name), today is \(day).";
}

func getGasPrices()->(Double,Double,Double){
return (3.59,3.69,3.69);
}

func sumOf(numbers:Int...)->Int{
var sum = 0
for number in numbers{
sum += number
}
return sum
}

func returnFifteen()->Int{
var y = 10;
func add(){
y+=5;
}
add();
return y;
}

var numbers = [1,2,3,4,5];
//var number = numbers.map{
//    (number:Int)->String in
//    if number%2 == 0 {
//        return String(number);
//    }else{
//        return "-1";
//    }
//};
var number = numbers.map({ number in 3*number});
print(number);

/////////////////////////////// 类

class Shape{
var numberOfSides = 0;

init(numberOfSides:Int){
self.numberOfSides = numberOfSides;
}
func simpleDescription() -> String{
return "A shaple with \(numberOfSides).";
}
}

var shape = Shape(numberOfSides: 1);
shape.numberOfSides = 7;
print(shape.simpleDescription());

/////////////////////////////数组
var shoppinglist2 = ["1","2","3"];
for (index,value) in shoppinglist2.enumerate(){
print("Item \(index+1):\(value)");
}

////////////////////////////字典
var mapDirectory = ["key1":"value1","key2":"value2"];
for(key,value) in mapDirectory{
print("key = \(key) and value = \(value)");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: