Swift语言基础笔记(二)

前端之家收集整理的这篇文章主要介绍了Swift语言基础笔记(二)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

上一篇 Swift语言基础笔记(一)介绍了整形、浮点型、布尔类型,这篇接着介绍下字符、字符串、元组、可选型等类型。

字符和字符串的使用

//: Playground - noun: a place where people can play

import UIKit

var str = "Hello,playground"
//以字符形式打印
for c in str.characters{
    print(c)
}

//初始化空字符串
var emptyStr = "";
var anotherEmptyStr = String();

str.uppercaseString;
str.lowercaseString;
str.capitalizedString;
str.containsString("Hello");
str.hasPrefix("He");
str.hasSuffix("und");
let s = "one third is \(1.0/3.0)";
//NSString的使用,它不是产unicode码,如中文处理不好。
//保留两位小数,占位符的使用,并转换为String
let s2: String = NSString(format: "one third is %0.2f",1.0/3.0) as String;

let s3: NSString = "one third is 0.33";
//从第几个开始,前闭后开
s3.substringFromIndex(2);
//从0开始到指定值前一个
s3.substringToIndex(5);
s3.substringWithRange(NSMakeRange(4,5));

let dog: Character = "

元组的使用

//元组 Tuple,多种基本类型的组合。
var point = (5,9);
var httpResponse = (404,"Not find");
var point2:(Int,Int,Float) = (2,4,8.0);
var httpResponse2:(Int,String) = (200,"OK");

var (statusCode,statusMessage) = httpResponse;
statusCode = 200;
statusMessage;

point.0;
point.1;
point2.2;

let point3 = (first: 3,second: "hello");
point3.first;
point3.second

let point4: (ff: Int,ee:Int) = (4,9);
point4.ee;
point4.ff;

let loginResult = (false,"deng");
let (isLoginSuccess,_) = loginResult;
if isLoginSuccess{
    print("Login success");
}else{
    print("Login Failed");
}

print(point4.ff,point4.ee,isLoginSuccess,"swift",separator:",",terminator:"?");
print("hello");
print("\(point4.ff) * \(point4.ee) = \(point4.ff * point4.ee)");
可选型
可选型中有一个不得不提的关键字nil,它代表一种类型,也是空的意思,只有显式声明为可选型时才能赋值为nil,可选型的声明是类型后加一个?号,或加!号声明隐式可选型。

optional是Swift新加入的类型,其它语言也没有这种类型,可简单理解为:有值时就是?前面的类型,无值时就是nil。

//: Playground - noun: a place where people can play

import UIKit
//可选型
var errorCode: Int? = 404;
errorCode = 0;
errorCode = nil;

var color:UIColor? = nil;

let imInt = 405;
errorCode = imInt;

print(errorCode);


var error:String? = "405";
print(error);

//可选型的解包
"The errorCode is " + error!;

if error != nil{
   "The errorCode is " + error!;
}else{
    "No error";
}

if let unWrappedErrorCode = error{
    "The errorCode is " + unWrappedErrorCode;
}

//解包可以几条命令放一起,用逗号分开
var errorMessage:String? = "Not found";
if let error = error,errorMessage = errorMessage{
    "The errorCode is " + error + "\n The errorMessage is " + errorMessage;
}

if let error = error,errorMessage = errorMessage where error == "405"{
        print("Page not found");
}
可选型的应用
//: Playground - noun: a place where people can play

import UIKit
//可选型的应用
var errorMessage:String? = "Not Found";
if let errorMessage = errorMessage{
    errorMessage.uppercaseString;
}

//先判断errorMessage是否为nil,如果不是就执行后面的方法
errorMessage?.uppercaseString;

//errorMessage!.uppercaseString;

var uppercaseErrorMessage = errorMessage?.uppercaseString;

if let errorMessage = errorMessage?.uppercaseString{
    errorMessage;
}


var error: String? = nil;
var error2: String? = "407";
let message: String;
if let error = error{
    message = error;
}else{
    message = "No error";
}

let message2 = error2 == nil ? "No error" : error2;

let message3 = error ?? "No error";
可选型在元组中的应用
//: Playground - noun: a place where people can play

import UIKit
//可选型在元组中的应用
var error: (errorCode: Int,errorMessage: String?) = (404,"No Found");
error.errorMessage = nil;
error;
//error = nil;

var error2: (errorCode: Int,errorMessage: String)? = (404,"No Found");

//error2.errorCode = 48;
error2 = nil;

var error3: (errorCode: Int,errorMessage: String?)? = (404,"No Found");

//可选型的实际应用
var ageInput: String = "dzt";
var age = Int(ageInput);

var a: String = "16";
var aa = Int(a);
if let aa = Int(a) where aa < 20{
    print("You're a teenager")
}

var greetings = "Hello"
greetings.rangeOfString("ll")
greetings.rangeOfString("oo")


//隐式可选型
//在类型后加!是定义隐式可选型,不需要解包也可以使用
var error1: String! = nil
error1 = "Not Found"
"The message " + error1
隐式可选型的应用
//: Playground - noun: a place where people can play

import UIKit
//隐式可选型的应用
class City {
    let cityName: String
    unowned var country: Country
    init(cityName:String,country: Country){
        self.cityName = cityName
        self.country = country
    }
}

class Country {
    let countryName: String
    var capitalCity: City!  //定义为隐式可选型,在初始化时赋值
    
    init(countryName: String,capitalCity: String){
        self.countryName = countryName;
        self.capitalCity = City(cityName: capitalCity,country: self)
    }
    
    func showInfo(){
        print("This is \(countryName)")
        print("The capital is \(capitalCity.cityName)")
    }
}

let china = Country(countryName: "China",capitalCity: "Beijing")
china.showInfo()

猜你在找的Swift相关文章