标签:
首先,学习这门语言是速学的,我不想浪费太多时间在笔记这门语言和其他语言的哪里不同,特性你自己亲自实践就知道了。下面是我亲自实践总结的一点点皮毛而已。
废话不多说,直接上代码:
1 声明变量和常量
var str: String = "Look what I can do"
var age: Int = 18
let favoriteNumber: Int = 42
代替上面的简单写法就是:
var str = "Look what I can do"
var age = 18
let favoriteNumber = 42
2 字符串声明和初始化或者说是直接赋值
var favoriteGame: String = "Mario Kart"
favoriteGame = "Super Smash Bros"
3 操作符的使用与if语法
let batmanCoolness = 10
var supermanCoolness = 9
let aduamanCoolness = 1
batmanCoolness < supermanCoolness
batmanCoolness > supermanCoolness
supermanCoolness >= 8
batmanCoolness == (supermanCoolness + aduamanCoolness)
batmanCoolness > aduamanCoolness && batmanCoolness == (aduamanCoolness + supermanCoolness)
batmanCoolness < supermanCoolness || aduamanCoolness < supermanCoolness
var spidermanCoolness = 7
(spidermanCoolness + aduamanCoolness) > supermanCoolness
spidermanCoolness == 7 && aduamanCoolness == 2
if(batmanCoolness > spidermanCoolness){
spidermanCoolness = spidermanCoolness - 1
}else if(batmanCoolness >= spidermanCoolness){
spidermanCoolness = spidermanCoolness - 1
}else{
spidermanCoolness = spidermanCoolness + 1
}
if(supermanCoolness < spidermanCoolness) {
supermanCoolness = supermanCoolness - 1
spidermanCoolness = spidermanCoolness + 1
} else if (supermanCoolness == spidermanCoolness) {
supermanCoolness = supermanCoolness + 1
spidermanCoolness = spidermanCoolness + 1
} else {
supermanCoolness = supermanCoolness + 1
spidermanCoolness = spidermanCoolness - 1
}
4 print 语法的使用
print("Hello,World")
print("Ry is awesome")
print(batmanCoolness)
5 字符串插入的使用
var apples = 5
print("Sally has \(apples)apples")
print("Sally has \(apples - 5) apples")
6 可选optional使用(注意:0或者一个具体值)
var optionalNumber: Int? = 5
optionalNumber = nil
if let number = optionalNumber {
print("It is a number")
}
else{
print("It is not a number")
}
7 类型转换
var languagesLearned: String = "3"
var languagesLearnedNum: Int? = Int(languagesLearned)
如:
var tutorialTeam: String = "55"
var editorialTeam = 23
var tutorialTeamNum: Int? = Int(tutorialTeam)
if let numTutorial = tutorialTeamNum {
if(numTutorial > editorialTeam) {
print("\(numTutorial) is greater than \(editorialTeam)")
} else if (numTutorial < editorialTeam) {
print("\(numTutorial) is less than \(editorialTeam)")
} else {
print("\(numTutorial) is equal to \(editorialTeam)")
}
} else {
print("Invalid entry")
}
8 方法的使用
例子:func randomIntBetween(low:Int, high:Int) -> Int {
let range = high - (low - 1)
return (Int(arc4random()) % range) + (low - 1)
}
新建项目PeopleDatabase
main.swift
import Foundation
//var newPerson = Person() //创建对象newPerson类的person,等于一个Person对象初始化。即可调用person类
//
//newPerson.enterInfo()
//newPerson.printInfo()
// 加入循环
var response: String
var people: [Person] = [] //创建数组
repeat{
var newPerson = Person()
newPerson.enterInfo()
newPerson.printInfo()
people.append(newPerson)
newPerson.printInfo()
print("Do you want to enter another name?(y/n)")
response = input()
}while(response == "y")
print("Number of people in the database: \(people.count)")
for onePerson in people {
onePerson.printInfo()
}
新建一个Person类
Person.swift
import Foundation
class Person {//创建了一个person的类,类不仅有属性,还可以包含方法
var firstName = ""
var lastName = ""
var age = 0
func input() -> String {
let keyboard = NSFileHandle.fileHandleWithStandardInput()
let inputData = keyboard.availableData
let rawString = NSString(data: inputData, encoding:NSUTF8StringEncoding)
if let string = rawString {
return string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
} else {
return "Invalid input"
}
}
func changeFirstName(newFirstName:String) { //1创建方法
firstName = newFirstName
}
func enterInfo() {//2创建方法
print("What is the first name?")
firstName = input()
print("What is \(firstName)‘s last name?")
lastName = input()
print("How old is \(firstName) \(lastName)")
let userInput = Int(input())
if let number = userInput {
age = number
}
}
func printInfo() {//3创建方法
// print("First Name: \(firstName)")
print("\(firstName) \(lastName) is \(age) years old")
}
}
记得导入文件helpers.swift的辅助文件
helpers.swif
import Foundation
func input() -> String {
let keyboard = NSFileHandle.fileHandleWithStandardInput()
let inputData = keyboard.availableData
let rawString = NSString(data: inputData, encoding:NSUTF8StringEncoding)
if let string = rawString {
return string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
} else {
return "Invalid input"
}
}
func randomIntBetween(low:Int, high:Int) -> Int {
let range = high - (low - 1)
return (Int(arc4random()) % range) + (low - 1)
}
标签:
原文地址:http://www.cnblogs.com/linxiu-0925/p/5483251.html