protocol ExampleProtocol {
var simpleDescription: String {
get
}
mutating func adjust()
}
class SimpleClass: ExampleProtocol {
var simpleDescription: String = "A very simple class."
var anotherProperty: Int = 69105
func adjust() {
simpleDescription += " Now 100% adjusted."
}
}
var a = SimpleClass()
a.adjust()
let aDescription = a.simpleDescription
struct SimpleStructure: ExampleProtocol {
var simpleDescription: String = "A simple structure"
mutating func adjust() {
simpleDescription += " (adjusted)"
}
}
var b = SimpleStructure()
b.adjust()
let bDescription = b.simpleDescription
protocol SomeProtocol{
}
class SomeClass : SomeProtocol{
}
protocol AnotherProtocol1{
static var property : Int { get set}
}
class AnotherClass1 : AnotherProtocol1{
class var property : Int {
get{
return 10
}
set{
}
}
}
protocol AnotherProtocol2{
func myFunc() -> Int
}
class AnotherClass2 : AnotherProtocol2{
func myFunc() -> Int {
return 10
}
}
protocol Togg{
mutating func togg()
}
enum OnOffSwitch : Togg{
case Off , On
mutating func togg() {
switch self{
case .Off:
self = On
case .On:
self = Off
}
}
}
var lightSwitch = OnOffSwitch.Off
lightSwitch.togg()
protocol MyRect{
func myLuckNumber() -> Int
}
class MyRectImp : MyRect{
func myLuckNumber() -> Int {
return 10
}
}
class Dice {
let sides :Int
var gener : MyRect
init(sides:Int, gener:MyRect){
self.sides = sides
self.gener = gener
}
}
var dice = Dice(sides: 6, gener: MyRectImp())
dice.gener.myLuckNumber()
extension Int: ExampleProtocol {
var simpleDescription: String {
return "The number \(self)"
}
mutating func adjust() {
self += 42
}
}
let protocolValue: ExampleProtocol = a
protocolValue.simpleDescription
extension Double{
var km : Double { return self * 1_000.0 }
var m : Double { return self }
var cm : Double { return self / 100.0 }
}
let jjLength = 1.m
let jjLength_km = 1.km
print(10.km + 1.m)
class MyClass{
var a : Int
init(){
a = 10
}
}
extension MyClass{
convenience init( parm:Int){
self.init()
print("扩展构造器--->便利构造器, \(parm)")
}
}
var myClass = MyClass(parm: 9)
extension Int{
func myIntFunc(){
print("值为\(self) , 哈哈哈哈!")
}
}
1.myIntFunc()
extension Double{
mutating func myMoidfySelfValue() {
self = self * self
}
}
var d = 2.0
d.myMoidfySelfValue()
extension Character {
enum Kind{
case Big
case Small
}
var k : Kind{
if(String(self).lowercaseString == "a"){
return Kind.Big
}else{
return Kind.Small
}
}
}
var ch : Character = "a"
ch.k
extension Int {
subscript(digitIndex: Int) -> Int {
var decimalBase = 1
for _ in 1...digitIndex {
decimalBase *= 10
}
return (self / decimalBase) % 10
}
}
746381295[0]
746381295[1]
746381295[2]
746381295[8]
746381295[9]
0746381295[9]
extension Character {
enum Kind1 {
case Vowel, Consonant, Other
}
var kind: Kind1 {
switch String(self).lowercaseString {
case "a", "e", "i", "o", "u":
return .Vowel
case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
"n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
return .Consonant
default:
return .Other
}
}
}
func printLetterKinds(word: String) {
print("'\\(word)' is made up of the following kinds of letters:")
for character in word.characters {
switch character.kind {
case .Vowel:
print("vowel ")
case .Consonant:
print("consonant ")
case .Other:
print("other ")
}
}
print("\n")
}
printLetterKinds("Hello")
<script type="text/javascript">
$(function () {
$('pre.prettyprint code').each(function () {
var lines = $(this).text().split('\n').length;
var $numbering = $('<ul/>').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('<li/>').text(i));
};
$numbering.fadeIn(1700);
});
});
</script>