标签:
let str = "23" //打印结果: "23"
let age : Int? = Int(str) //打印结果 :23
let path : String? = NSBundle.mainBundle().pathForResource("xiaofeng.plist", ofType: nil) //打印的结果为nil,因为写程序的时候并没有新建一个名字为"xiaofeng"的plist文件
if let path = path {
NSArray(contentsOfFile: path)
}
let url : NSURL? = NSURL(string: "www.baidu.com") //打印结果:www.baidu.com
if let url = url {
NSURLRequest(URL : url) //<NSURLRequest: 0x7fd050d18610> { URL: www.baidu.com }
}
//url中出现了中文,并且没有用可选类型来接收值,系统会报错
//系统包的错误: cannot convert value of type ‘NSURL?‘ to specified type ‘String‘
let url : String = NSURL(string: "www.baidu.com/百度")
let path1 = NSBundle.mainBundle().pathForResource("123.plist", ofType: nil)
let array = [12,"xiaofeng",1.89]
let arrayM = [NSObject]()//定义数组,内部的元素是NSObject类型
//判断取出的第一个元素是不是整型
if let firstObject = array.first {
if firstObject is Int {
print("是Int类型") //打印结果:"是Int类型\n"
}else{
print("不是Int类型")
}
}
let dict = ["name" : "xiaofeng", "age" : 20, "height" : 1.88]
let value = dict["name"] //通过按住option点击value,可以看出类型为可选类型
//将NSObject转成String类型
if let value = value {
//此处value as? String也有可能转失败
let valueStr : String? = value as? String
//判断是否能转成功
if let valueStr = valueStr {
let info = "my name is " + valueStr //最终结果:"my name is xiaofeng"
}
}
if let valueStr = value as? String {
let info = "my name is " + valueStr //打印结果 :"my name is xiaofeng"
}
let info = "my name is " + (value as! String) //打印结果 :"my name is xiaofeng"
有参数有返回值
- (CGFloat)sum:(CGFloat)num1 num2:(CGFloat)num2;
有参数没有返回值
- (void)sum1:(CGFloat)num1 num2:(CGFloat)num2;
没有参数有返回值
- (CGFloat)sum2;
没有参数没有返回值
- (void)sum3;
func 函数名(参数列表) -> 返回值类型 {
代码块
return 返回值
}
func sum (num1 : Int , num2 : Int) ->Int {
return num1 + num2
}
let result = sum(10, num2: 10)
print("计算的结果是:\(result)") //打印结果 : "计算的结果是:20\n"
func minus (num1 : Double , num2 : Double) {
print(num1 - num2) //打印出来的结果 : "2.0\n"
}
minus(22.1, num2: 20.1)
func multiply() ->Int {
return 10 //返回结果 : 10
}
multiply()
func divide() {
}
divide()
let array = [11,22,44,33,66,88]
var oddCount = 0 //奇数
var evenCount = 0 //偶数
for num in array {
if num % 2 == 0 {
evenCount++ //打印结果 : 4
} else {
oddCount++ //打印结果 : 2
}
}
let nums = [11,22,33,44,55,7,77,88]
func getCount (nums : [Int]) ->(Int,Int) {
var evenCount = 0 //偶数
var oddCount = 0 //奇数
for num in nums {
if num % 2 == 0 {
evenCount++ //打印结果 : 3
}else {
oddCount++ //打印结果 : 5
}
}
return (evenCount,oddCount)
}
let count = getCount(nums)
print("偶数的个数是:\(count.0),奇数的个数是:\(count.1)")
func sum (num1 : Int , num2 : Int , num3 : Int) ->Int {
print(num1)
print(num2)
print(num3)
return num1 + num2 + num3 //返回结果 : 30
}
//调用
sum(10, num2: 10, num3: 10)
func multiply (num1 num1 : Int , num2 : Int , num3 : Int) ->Int {
return num1 * num2 * num3 //返回结果 : 1000
}
multiply(num1: 10, num2: 10, num3: 10)
func subtraction (num1 : Int , _ num2 : Int , _ num3 : Int) ->Int {
return num1 - num2 - num3 //返回结果 : 50
}
subtraction(100, 20, 30)
func makeCoffee(coffeeName : String = "蓝山") ->String {
return "制作了一杯:\(coffeeName)咖啡"
}
makeCoffee("拿铁") //通过传入咖啡的名称,返回制作好的咖啡
makeCoffee() //此时的调用函数,返回的结果是默认设置的"蓝山"咖啡
//此时需要些三个方法来计算
func sum(num1 : Int , num2 : Int) ->Int {
return num1 + num2
}
func sum(num1 : Int , num2 : Int , num3 : Int) ->Int {
return num1 + num2 + num3
}
func sum(num1 : Int , num2 : Int , num3 : Int , num4 : Int) ->Int {
return num1 + num2 + num3 + num4
}
sum(10, num2: 20, num3: 30, num4: 40)
//计算参数为数组中的各个元素的和(方法一)
func arrayCount(nums : [Int]) ->Int {
var result = 0
for num in nums {
result += num
}
return result
}
arrayCount([10,20,30,40]) //传入一个数组 打印结果是 : 100
//计算参数为数组中的各个元素的和(方法二)
func sunNums(nums : Int...) ->Int {
var result = 0
for num in nums {
result += num
}
return result
}
sunNums(10,20,30,40)
var m = 20
var n = 30
func exchangeNum(var num1 : Int , var num2 : Int) {
let tempNum = num1
num1 = num2
num2 = tempNum
}
exchangeNum(m, num2: n)
print("m :\(m), n: \(n)") //打印出结果 : "m :20, n: 30\n"
var a = 20
var b = 30
func exchangeNum(inout num1 : Int , inout num2 : Int) {
let tempNum = num1
num1 = num2
num2 = tempNum
}
exchangeNum(&a, num2: &b)
print("a : \(a) , b : \(b)") //打印结果 : "a : 30 , b : 20\n"
var c = 20
var d = 30
func exchangeNum( num1 : Int , num2 : Int) -> (Int , Int) {
return (num2 ,num1)
}
let num = exchangeNum(c, num2: d)
c = num.0 //打印结果 : 30
d = num.1 //打印结果 : 20
func test() {
func demo() {
print("demo")
}
demo() //先打印:demo
print("test") //再打印:test
// demo() //如果在此处调用 先打印:test 再打印:demo
}
test()
func add(a : Int , b : Int) ->Int {
return a + b
}
func mul(a : Int , b : Int) ->Int {
return a * b
}
var a = add //打印出函数的类型 : (Int, Int) -> Int
var a : (Int, Int) -> Int = add
a(20,30) //打印结果 : 50
a = mul //打印结果 : (Int, Int) -> Int
a(30,60) //打印结果 : 1800
var m : Int = 20
func test(num : Int) ->Int {
return num
}
test(m) //打印结果 : 20
test(30) //打印结果 : 30
func demo(funcName : (Int) ->Int) {
funcName(20)
}
demo(test)
func add(a : Int , b : Int) ->Int {
return a + b
}
func demo1() ->((Int, Int) ->Int) {//返回值的类型必须是和返回函数同种类型
return add
}
let tempFunc = demo1()
tempFunc(20,30)
typedef enum {
SexMan, //默认为0
SexWoMan //1
}Sex
enum SomeEnumeration {
// 属性(enumeration definition goes here)
}
//单个值一行
enum Sex {
case Man
case WoMan
}
//多个值一行
enum Direction {
case East,West,South,North
}
var sex = Sex.Man
sex = .WoMan
enum Planet : Int {
case Mercury = 0, venus, Earth, Mars
}
enum Sex1 : Int {
case Man = 0
case WoMan = 1
}
enum Sex2 : String {
case Man = "男"
case WoMan = "女"
}
let sex1 = Sex1(rawValue: 0) //打印出结果 : Man
let sex2 = Sex1(rawValue: 1) //打印出结果 : WoMan
let sex4 = Sex2(rawValue: "男") //打印出结果 : Man
let sex5 = Sex2(rawValue: "女") //打印出结果 : WoMan
//1.定义中心点
let centerX : Double = 100
let centerY : Double = 100
//获取比较的点
let x : Double = 60
let y : Double = 50
let x1 : Double = 200
let y1 : Double = 150
//定义方法,用来比较两个点
func inRange(x : Double, y : Double) ->Bool {
//获取两个点之间的距离
let disX = x - centerX
let disY = y - centerY
//计算第三边的距离
//pow(disX, 2)代表了disX的2次方;sqrt代表开方根
let distance = sqrt(pow(disX, 2) + pow(disY, 2))
//判断是否小于200
return distance < 200 //打印结果 : true
}
inRange(60, y: 50)
//结构体定义
struct Location {
var x : Double
var y : Double
}
//通过结构体创建对应的点
let center = Location(x: 100, y: 100)
//需要判断的点
let testLocation = Location(x: 50, y: 40)
//定义函数
func inRang(location : Location) ->Bool {
let disX = location.x - center.x
let disY = location.y - center.y
let distance = sqrt(pow(disX, 2) + pow(disY, 2))
return distance < 200
}
//方法的调用
inRang(testLocation)
struct Location {
var x : Double = 0
var y : Double = 0
var z : Double = 0
//init()是系统的构造方法
init() {
}
//自定义构造方法
init(x : Double, y : Double, z : Double) {
self.x = x
self.y = y
self.z = z
}
}
Location()
Location() //如果init()构造方法没有被重写,是无法敲出来的
//创建Location实例
let location = Location(x: 100, y: 100, z: 100)
let point = CGPoint(x: 100, y: 100)
let size = CGSize(width: 100, height: 100)
let rect = CGRect(origin: point, size: size)
var rect1 = CGRect().origin.x
rect1 = 100
let rect2 = CGRect(x: 0, y: 0, width: 100, height: 100)
let range = NSRange(location: 3, length: 9)
struct Location {
var x : Double
var y : Double
//mutating少了是会报错的
mutating func moveH(dis : Double) {
x += dis
}
mutating func moveV(dis : Double) {
y += dis
}
}
var location = Location(x: 50, y: 60)
location.moveH(-100)
location.moveV(100)
print(location) //打印出结果 : "Location(x: -50.0, y: 160.0)\n"
extension CGPoint {
mutating func moveH(dis : CGFloat) {
x += dis
}
mutating func moveV(dis : CGFloat) {
y += dis
}
}
var point = CGPoint(x: 100, y: 100)
point.moveH(100)
print(point) //打印出结果 : "(200.0, 100.0)\n"
extension UIButton {
func getTitle() ->String? {
return titleLabel?.text
}
}
let btn = UIButton()
btn.setTitle("按钮", forState: .Normal)
print(btn.getTitle()) //打印出结果 : "Optional("按钮")\n"
class 类名 : SuperClass {
// 定义属性和方法
}
class Person: NSObject {
var name = ""
var age = 0
var mathScore = 0.0
var chineseScroe = 0.0
}
//创建Person对象
let p = Person()
//给存储属性赋值
p.name = "xiaofeng"
p.age = 19
p.mathScore = 90
p.chineseScroe = 100
class Person: NSObject {
var name = ""
var age = 0
var mathScore = 0.0
var chineseScroe = 0.0
//计算属性(本质是一个方法)
func getAverageScore() ->Double {
return (mathScore + chineseScroe) * 0.5
}
}
//创建Person对象
let p = Person()
//给存储属性赋值
p.name = "xiaofeng"
p.age = 19
p.mathScore = 90
p.chineseScroe = 100;
//调用方法
print(p.getAverageScore()) //打印结果 : "95.0\n"
var averageScore : Double {
get {
return (mathScore + chineseScroe) * 0.5
}
set (newScore){
chineseScroe = 2 * newScore - mathScore
}
}
var averageScore : Double {
return (mathScore + chineseScroe) * 0.5
}
static var courseCount = 0
//设置课程个数
Person.courseCount = 3
//取出类属性的是
print(Person.courseCount)
class Person {
var name : String = ""
var age : Int = 0
//系统的构造函数
init() {
}
//添加构造函数
init(name : String, age : Int) {
self.name = name
self.age = age
}
//用字典添加
init(dict : [String : NSObject]) {
if let name = dict["name"] as? String {
self.name = name
}
if let age = dict["age"] as? Int {
self.age = age
}
}
}
//2.创建类对象
let p = Person()//通过普通方法创建
//通过扩充的方法创建
let p1 = Person(dict: ["name" : "xiaofeng", "age" : 19])
class Person : NSObject{
var name : String = ""
var age : Int = 0
init(dict :[String : NSObject]) {
//必须先初始化对象
super.init()
setValuesForKeysWithDictionary(dict)
}
//如果使用KVC的时候,发现模型属性值并不能一一对应,此时使用kvc是会报错的,但是重写下面这个方法可以避免系统报错
override func setValue(value: AnyObject?, forUndefinedKey key: String) {
}
}
let p = Person(dict: ["name" : "xiaofeng", "age" : 18])
print(p.name) //打印结果 : "xiaofeng\n"
print(p.age) //打印结果 : "18\n"
deinit {
// 执行析构过程
}
class Person : NSObject {
var name = ""
var age = 0
//用来释放一些没用的资源,类似于delloc
deinit {
print("Person -----deinit") //打印结果 : "Person -----deinit\n"
NSNotificationCenter.defaultCenter().removeObserver(self)
}
}
var p : Person? = Person()
p = nil //此时将p赋值为nil,析构函数中的print会打印Person -----deinit
class Person: NSObject {
//属性监听器name
var name : String = "" {
//监听属性即将发生改变
willSet (new) {
//在该方法中有一个默认的系统属性newValue,用于存储新值
print(name) //打印结果 : "\n"
print(new) //打印结果 : "xiaofeng\n"
}
//监听属性已经发生改变
didSet (old){
//在该方法中有一个默认的系统属性oldValue,用于存储旧值
print(name) //打印结果 : "xiaofeng\n"
print(old) //打印结果 : "\n"
}
}
//属性监听器age
var age : Int = 0 {
//在willSet中有newValue,用于保存新值
willSet (newAge) {
print(age)
print(newAge)
}
//在didSet中有oldSet,用于保存旧值
didSet (oldAge) {
print(age)
print(oldAge)
}
}
}
//创建对象
let p = Person()
p.name = "xiaofeng"
p.age = 20
标签:
原文地址:http://blog.csdn.net/xf931456371/article/details/51247282