ios -- 教你如何轻松学习Swift语法(二)
var name : Optional<String> = nil
var name : String? = nil
name = Optional("why")
name = "why" // 系统会对字符串进行包装Optional, 再进行赋值
print(name!)
if let name = name {
print(name)
print(name)
}


let urlString = "www.baidu.com" let url : NSURL? = NSURL(string: urlString)
if let url = url {
let request = NSURLRequest(URL: url)
}
let path = NSBundle.mainBundle().pathForResource("123.plist", ofType: nil) if let path = path {
NSArray(contentsOfFile:path)
}
//1.is的使用
let infoArray = ["why" , , 1.98]
let item = infoArray[] //item.isKindOfClass(UIButton.self) //string是结构体,不能用isKindOfClass
if item is String {
print("是字符串")
}else {
print("不是字符串")
}
let urlString = "www.baidu.com"
(urlString as NSString).substringToIndex()
let item1 = infoArray[]
let name = item1 as? String
if let name = name {
print(name.characters.count)
}
简写:
if let name = infoArray[] as? String {
print(name.characters.count)
}
let count = (infoArray[] as! String).characters.count
}




// 函数的嵌套
let value =
func test() {
func demo() {
print("demo \(value)")
}
print("test")
demo()
}
demo() // 错误 必须在对应的作用域内调用
test() // 执行函数会先打印'test',再打印'demo'
// 定义两个函数
func addTwoInts(a : Int, b : Int) -> Int {
return a + b
} func multiplyTwoInt(a : Int, b : Int) -> Int {
return a * b
}
// 定义函数的类型
var mathFunction : (Int, Int) -> Int = addTwoInts // 使用函数的名称
mathFunction(, ) // 给函数的标识符赋值其他值
mathFunction = multiplyTwoInt // 使用函数的名称
mathFunction(, )
// 3.将函数的类型作为方法的参数
func printResult(a : Int, b : Int, calculateMethod : (Int, Int) -> Int) {
print(calculateMethod(a, b))
} printResult(, b: , calculateMethod: addTwoInts)
printResult(, b: , calculateMethod: multiplyTwoInt)
// 1.定义两个函数
func stepForward(num : Int) -> Int {
return num +
} func stepBackward(num : Int) -> Int {
return num -
} // 2.定义一个变量,希望该变量经过计算得到0
var num = - // 3.定义获取哪一个函数
func getOprationMethod(num : Int) -> (Int) -> Int {
return num <= ? stepForward : stepBackward
} // 4.for循环进行操作
while num != {
let oprationMethod = getOprationMethod(num)
num = oprationMethod(num)
print(num)
}
enum <#name#> {
case <#case#>
}
enum Planet {
case Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
}
enum Direction : Int{
case East = , West , North , Sourth
}
//完整写法
let d : Direction = Direction.North
//简单写法:根据上下文能推导出确定的类型
var d1 = Direction.East
d1 = .West //枚举类型的使用
let btn = UIButton(type: .Custom)
let btn = UIButton(type: .Custom) enum Direction : String{
case East = ""
case West = ""
case North = ""
case Sourth = ""
}
var b : Direction = .West let a = Direction(rawValue: “”)
enum Direction2 : Int{
case East = , West , North , Sourth
//只要给第一个成员赋值,会自动按照递增的方式给后面的成员赋值
//相当于 West = 2, North = 3, Sourth = 4
//注意:这种赋值方法只对整型有效,赋值其它类型无效
struct 结构体名称 {
// 属性和方法



class 类名 : SuperClass {
// 定义属性和方法
}
class Person {
var name : String = ""
var age : Int =
}
let p = Person()
p.name = "lkj"
p.age =


class Person {
var name : String
var age : Int
// 自定义构造函数,会覆盖init()函数
init(name : String, age : Int) {
// 如果在一个方法中, 属性名称产生了歧义(重名), self.不可以省略
self.name = name
self.age = age
}
}
// 创建一个Person对象
let p = Person(name: "why", age: ) class Person: NSObject {
var name : String
var age : Int
// 重写了NSObject(父类)的构造方法 在init前面加上override
override init() {
name = ""
age =
}
}
// 创建一个Person对象
let p = Person()
class Person: NSObject {
// 结构体或者类的类型,必须是可选类型.因为不能保证一定会赋值
var name : String?
// 基本数据类型不能是可选类型,否则KVC无法转化
var age : Int =
// 自定义构造函数,会覆盖init()函数
init(dict : [String : NSObject]) {
// 必须先初始化对象
super.init()
// 调用对象的KVC方法字典转模型
setValuesForKeysWithDictionary(dict)
}
//如果字典中某些键值对,在类中找不到对应的属性,就会报错
//不想让它报错,可以重写setValue forUndefinedKey key:
override func setValue(value: AnyObject?, forUndefinedKey key: String) {
}
}
// 创建一个Person对象
let dict = ["name" : "why", "age" : ]
let p = Person(dict: dict)
deinit {
// 执行析构过程
}
class Person {
var name : String
var age : Int init(name : String, age : Int) {
self.name = name
self.age = age
} deinit {
print("Person-deinit")
}
} var p : Person? = Person(name: "why", age: )
p = nil
ios -- 教你如何轻松学习Swift语法(二)的更多相关文章
- ios -- 教你如何轻松学习Swift语法(一)
目前随着公司开发模式的变更,swift也显得越发重要,相对来说,swift语言更加简洁,严谨.但对于我来说,感觉swift细节的处理很繁琐,可能是还没适应的缘故吧.基本每写一句代码,都要对变量的数据类 ...
- ios -- 教你如何轻松学习Swift语法(三) 完结篇
前言:swift语法基础篇(二)来了,想学习swift的朋友可以拿去参考哦,有兴趣可以相互探讨,共同学习哦. 一.自动引用计数 1.自动引用计数工作机制 1.1 swift和o ...
- 一步一步学习Swift之(二):好玩的工具playground与swfit基础语法
playground好于在于能一边写代码一边看到输出的常量变量的值.不需要运行模拟器. 我们来试一下该工具的用法. 打开xcode6开发工具,选择Get started with a playgrou ...
- IOS之Foundation之探究学习Swift实用基础整理<一>
import Foundation //加载网络数据,查找数据的字符串 let dataurl = "http://api.k780.com:88/?app=weather.city& ...
- JavaScript学习笔记--语法二
条件判断与C语言一样 两种循环.for 循环和 while 循环,JavaScript不区分整数和浮点数,统一用Number表示,所以不是 int i var x = 0; var i; for (i ...
- 轻松学习JavaScript十二:JavaScript基于面向对象之创建对象(二)
四原型方式 我们创建的每一个函数都有一个通过prototype(原型)属性.这个属性是一个对象,它的用途是包括能够由特定类型 的全部实例共享的属性和方法. 逻辑上能够这么理解:prototypt通过条 ...
- 轻松学习JavaScript十二:JavaScript基于面向对象之创建对象(一)
这一次我们深入的学习一下JavaScript面向对象技术,在学习之前,必要的说明一下一些面向对象的一些术语. 这也是全部面对对象语言所拥有的共同点.有这样几个面向对象术语: 对象 ECMA-262把对 ...
- 轻松学习Ionic (二) 为Android项目集成Crosswalk(更新官方命令行工具)
现在集成crosswalk不用这么麻烦了!官方的命令行工具已经能让我们一步到位,省去很多工作,只需在cmd中进入项目所在目录(不能有中文目录,还得FQ),执行: ionic browser a ...
- less学习-语法(二)
变量 @color1:#fff; 选择器 // Variables @mySelector: banner; // Usage .@{mySelector} { font-weight: bold; ...
随机推荐
- CF 335B. Palindrome(DP)
题目链接 挺好玩的一个题,1Y... #include <cstdio> #include <cstring> #include <iostream> using ...
- 【noiOJ】p1759
1759:最长上升子序列 查看 提交 统计 提问 总时间限制: 2000ms 内存限制: 65536kB 描述 一个数的序列bi,当b1 < b2 < ... < bS的时候,我 ...
- BZOJ2453维护队列&&BZOJ2120数颜色
2016-05-28 11:20:22 共同的思路: 维护某种颜色上一次在哪里出现pre,可以知道当pre<询问的l时更新答案 块内按照pre排序 修改的时候重新O(n)扫一遍,如果和之前的不一 ...
- js小效果-双色球
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...
- codeforces589J 简单dfs,队列
J. Cleaner Robot time limit per test 2 seconds memory limit per test 512 megabytes input standard in ...
- nginx“虚拟目录”不支持php的解决办法
这几天在配置Nginx,PHP用FastCGI,想装一个phpMyAdmin管理数据库,phpMyAdmin不想放在网站根目录 下,这样不容易和网站应用混在一起,这样phpMyAdmin的目录就放在别 ...
- 打造移动终端的 WebApp(一):搭建一个舞台
最近随着 Apple iOS 和 Android 平台的盛行,一个新的名词 WebApp 也逐渐火了起来,这里我也趁着热潮做一个关于 WebApp 系列的学习笔记,分享平时的一些研究以及项目中的经验, ...
- User Agent跨站攻击
看见有人发帖咨询这个问题http://zone.wooyun.org/content/17658 我就抛砖引玉下,这个案例就是refer被执行了,我有过多起案例 平时上网我们还可以修改浏览器user- ...
- MVC概念性的内容
MVC: 是一个缩写(model + view + control), Model:是一些类文件, 功能:负责增删改查, 负责跟数据库打交道 (把数据存入到数据库: 从数据库把数据读 ...
- The Earth Mover's Distance
The EMD is based on the minimal cost that must be paid to transform one distribution into the other. ...