class Fruit {
var weight = 0.0
func info()
{
println("我是一个水果,重\(weight)")
}
}
class Apple : Fruit {
var name : String!
func taste()
{
println("\(name)吃起来很好吃")
}
}
var a = Apple()
a.weight = 56
a.name = "红富士"
a.info()
a.taste()
class Bird {
func fly()
{
println("我在天空里自由自在的飞翔。。。")
}
}
class Ostrich : Bird {
override func fly() {
println("我只能在地上跑")
}
}
var o = Ostrich()
o.fly()
class Bird1 {
var speed : Double = 0
}
class Ostrich1 : Bird1 {
override var speed : Double {
get {
println("正在访问被重写的属性")
return super.speed
}
set {
super.speed = newValue * newValue
}
}
}
var os = Ostrich1()
os.speed = 20.0
println(os.speed)
class Ostrich2 : Bird1 {
override var speed : Double {
didSet{
super.speed *= speed
}
}
}
var os1 = Ostrich2()
os1.speed = 20.0
println(os1.speed)
class Base {
subscript (idx : Int) -> Int {
get {
println("父类的下标的get方法")
return idx + 10
}
}
}
class Sub: Base {
override subscript(idx : Int) ->Int {
get{
println("重写后的get下标")
println("通过super访问被重写之前的下标:\(super[idx])")
return idx * idx
}
set {
println("重写后的下标的setter方法传入的参数\(newValue)")
}
}
}
var base = Base()
println(base[7])
var sub = Sub()
println(sub[7])
class Apple1 {
var name : String!
var weight : Double
init(name: String,weight: Double)
{
self.name = name
self.weight = weight
}
convenience init(n name: String,w weight: Double)
{
self.init(name:name,weight:weight)
self.name = name
}
}
var app1 = Apple1(n: "junjun",w: 12)
println(app1.name)
var app2 = Apple1(name: "yuyu",weight: 13)
println(app2.name)
class Fruit1 {
var name : String
init(name: String)
{
self.name = name
}
}
class Apple5: Fruit1 {
var color : String
init (color: String)
{
self.color = color
println(self.color)
self.color = "红色"
super.init(name: "红花果")
println(super.name)
super.name = "红富士"
}
}
var aa = Apple5(color: "绿色")
println(aa.name)
@objc protocol TextProtocol{}
let hello : NSObject = "hello"
println("hello 是String \(hello is NSString)")
println("hello 是 NSDate \(hello is NSDate)")
var anyArray : [Any] = ["swift",29,["any":90],"JiangJun"]
for element in anyArray {
if element is NSString{
println(element)
}
}
var anyObjectArray : [AnyObject] = [Apple5(color: "红色"),Apple5(color: "蓝色")]
extension String
{
static var data : [String:Any] = [:]
var length: Int {
get{
return count(self)
}
set{
var originLength = count(self)
if newValue > originLength {
for idx in 1...originLength {
self += ""
}
}
else if newValue < originLength
{
var tmp = ""
var count = 0
for ch in self
{
tmp = "\(tmp) \(ch)"
count++
if count == newValue{
break
}
}
self = tmp
}
}
}
}
String.data["swift"] = 89
String.data["OC"] = 92
println(String.data)
var s = "fkit,org"
println(s.length)
s.length = 5
println(s)
s.length = 20
println(s)
*/
extension String
{
func subsrtingFromStrart(start : Int,toEnd : Int) -> String
{
var count = 0
var tmp = ""
for ch in self
{
if count >= start
{
tmp = "\(tmp)\(ch)"
}
if count >= toEnd - 1
{
break
}
count++
}
return tmp
}
static func valueOf(value : Bool) -> String {
return "\(value)"
}
}
var str = "fkit.orgisagoodcenter"
var subStr = str.subsrtingFromStrart(5,toEnd: 10)
println(subStr)
println(String.valueOf(true));
extension Array
{
mutating func retainAll(array : [T],compartor : (T,T) -> Bool)
{
var tmp = [T]()
for ele in self
{
for target in array
{
if compartor(ele,target)
{
tmp.append(ele)
break
}
}
}
self = tmp
}
}
var books = ["ios","Android","swift","Java","Rudy"]
books.retainAll(["Android","xuexi"]){
return $0 == $1
}
println(books)
struct SomeStruct
{
var name : String
var count : Int
}
extension SomeStruct
{
init(name:String)
{
self.name = name
self.count = 0
}
init(count:Int)
{
self.count = count
self.name = ""
}
}
var sc1 = SomeStruct(name: "fkit",count: 3)
var sc2 = SomeStruct(name: "JiangJun")
var sc3 = SomeStruct(count: 5)
extension String
{
enum Suit : String
{
case Diamond = "♦️"
case Clud = "♣️"
case Heart = "❤️"
}
static func judgeSuit(s: String) -> Suit?
{
switch(s)
{
case "♦️":
return .Diamond
case "♣️":
return .Clud
case "❤️":
return .Heart
default:
return nil
}
}
}
var s1: String.Suit? = String.judgeSuit("❤️")
println(s1)
var s2: String.Suit? = String.judgeSuit("j")
println(s2)
protocol Strokable
{
var strokeWidth : Double{get set}
}
protocol Fullable
{
var fullColor : Color? {get set}
}
enum Color
{
case Red,Green,Blue,Yellow,Cyan
}
protocol HasArea : Fullable,Strokable
{
var area : Double {get}
}
protocol Mathable
{
static var pi: Double {get}
static var e: Double {get}
}
struct Rect: HasArea,Mathable
{
var width: Double
var height: Double
init(width:Double,height:Double)
{
self.width = width
self.height = height
}
var fullColor: Color?
var strokeWidth: Double = 0.0
var area: Double {
get{
return width * height
}
}
static var pi: Double = 3.14159535
static var e: Double = 2.71828
}
class Circle: HasArea,Mathable
{
var radius:Double
init (radius:Double)
{
self.radius = radius
}
var fullColor:Color?
var strokeWidth:Double = 0.0
var area: Double {
get{
return Circle.pi * radius * radius
}
}
class var pi:Double {return 3.14159535}
class var e:Double {return 2.71828}
}
var rect = Rect(width: 4.5,height: 4.0)
println(rect.area)
rect.fullColor = Color.Red
rect.strokeWidth = 1.0
println(rect.fullColor)
println(rect.strokeWidth)
var circle = Circle(radius: 2.0)
println(circle.area)
circle.fullColor = Color.Green
circle.strokeWidth = 0.5
protocol Eatable
{
func taste()
static func test(msgs: String...)
}
struct Pie : Eatable
{
var weight:Double
func taste() {
println("\(weight)斤饼干吃起来香脆可口")
}
static func test(msgs: String...) {
println("饼干实现的test方法")
for msg in msgs
{
println("个数可变的形参:\(msg)")
}
}
}
class Apple1:Eatable
{
var name:String
init(name:String)
{
self.name = name
}
func taste() {
println("\(name)水分充足,营养丰富")
}
class func test(msgs: String...) {
println("苹果实现的test方法")
for msg in msgs
{
println("个数可变的形参:\(msg)")
}
}
}
Pie.test("饼干1","饼干2","饼干3")
var pie = Pie(weight: 2.3)
pie.taste()
Apple1.test("苹果1","苹果2","苹果3")
var apple = Apple1(name: "红富士")
apple.taste()
protocol Incrementable
{
mutating func incrementByDelta(delta:Double)
}
struct FkRange:Incrementable
{
var start: Double
var length: Double
mutating func incrementByDelta(delta: Double) {
self.length += delta
}
}
class Circle1 : Incrementable
{
var radius: Double
init(radius:Double){
self.radius = radius
}
func incrementByDelta(delta: Double) {
self.radius += delta
}
}
var range = FkRange(start: 2.0,length: 10.0)
range.incrementByDelta(5.0)
println(range.length)
var circle1 = Circle1(radius: 3.0)
circle1.incrementByDelta(5.0)
println(circle1.radius)
protocol Mathable3
{
subscript(idx:Int) -> Int {get}
subscript(a: Int,b: Int) -> Int {get}
}
struct LinearStruct : Mathable3
{
var factor: Int
subscript(idx: Int) ->Int
{
get{
return factor * idx
}
set{
println("执行LinearStruct的下标赋值")
}
}
subscript(a: Int,b: Int) -> Int
{
return factor * a + b
}
}
class Quadratic: Mathable3
{
var factor : Int
init(factor: Int)
{
self.factor = factor
}
subscript(idx: Int) -> Int
{
return factor * factor * idx
}
subscript(a: Int,b: Int) -> Int
{
return factor * factor * a + b
}
}
var q = Quadratic(factor: 5)
println(q[4])
println(q[4,6])
var line = LinearStruct(factor: 5)
println(line[4])
println(line[4,6])
protocol Initable
{
init(name: String)
init(name: String,weight: Double)
}
struct Bag : Initable
{
var name: String
var weight: Double
init(name: String)
{
self.name = name
self.weight = 0.0
}
init(name: String,weight: Double)
{
self.init(name:name)
self.weight = weight
}
}
class Fruit1
{
var name: String
init(name: String)
{
self.name = name
}
}
class Apple8 : Fruit1,Initable
{
var weight : Double
override required init(name: String)
{
self.weight = 0.0
super.init(name: name)
}
required convenience init(name: String,weight: Double) {
self.init(name: name)
self.weight = weight
}
}
var ap1 = Apple8(name: "红富士")
var ap2 = Apple8(name: "无花果",weight: 2.3)
println("\(ap2.name),\(ap2.weight)")
var bag1 = Bag(name: "书包")
var bag2 = Bag(name: "旅行包",weight: 20)
println("\(bag2.name),\(bag1.name)")
func test1(arg: protocol<HasArea,Mathable3>)
{
println("arg的填充颜色是\(arg.fullColor)")
println("arg的描边笔的粗细是\(arg.strokeWidth)")
println("arg参数的面积是\(arg.area)")
}
var circle5 = Circle(radius: 1.2)
circle5.strokeWidth = 0.5
circle5.fullColor = Color.Red
var rect5 = Rect(width: 3.4,height: 2.8)
rect5.strokeWidth = 0.8
rect5.fullColor = Color.Green
protocol Eatable1
{
func taste()
}
extension String : Eatable1
{
func taste() {
println("\(self)吃起来味道不错,呵呵")
}
}
func eat(foods : Eatable1...)
{
for food in foods
{
food.taste()
}
}
eat("swift","oc")
protocol Movable : class
{
func move()
}
class Car : Movable {
func move() {
println("汽车在飞")
}
}
var car1 = Car()
car1.move()
@objc protocol MyProtocol
{
optional var status : String {get}
optional func increment(val: Int)
optional subscript(idx: Int) -> Int{get}
}
class EmptyClass : MyProtocol {
}
class MyClass : MyProtocol
{
var name : String
init(name : String)
{
self.name = name
}
var status : String
{
if count(name) < 10
{
return "良好"
}
else
{
return "超长"
}
}
func increment(val: Int) {
println("系统正在增长长度")
for idx in 1...val
{
name += "="
}
}
}
var mp : MyProtocol = MyClass(name: "JiangJun")
println(mp.status)
mp.increment?(10)
println(mp.status)
class Person
{
var name : String
var age : Int
init(name: String,age : Int)
{
self.name = name
self.age = age
}
var description : String {
return "name = \(self.name),age = \(age)"
}
}
var person = Person(name: "悟空",age: 500)
println(person)
println(person.description)