Register for your free account! | Forgot your password?

You last visited: Today at 22:58

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[Swift] Snippets

Discussion on [Swift] Snippets within the Coding Snippets forum part of the Coding Releases category.

Reply
 
Old   #1

 
snow's Avatar
 
elite*gold: 724
Join Date: Mar 2011
Posts: 10,480
Received Thanks: 3,319
[Swift] Snippets

Wie evtl. einige mitbekommen haben, hat Apple vor knapp 2 Wochen eine neue Programmiersprache vorgestellt, die momentan in der Beta ist.
Ich schreibe bereits mein erstes Projekt damit & dachte mir, dass ich mal ein paar (gekürzte) Snippets daraus veröffentliche.

Das ganze ohne Copyright etc. & rechtlich ist das ganze auch okay, Swift unterliegt keinem NDA.

XPath Queries mit Hilfe von TFHpple auswerten:

Code:
let parser = TFHpple(HTMLData: html)
let summary = parser.searchWithXPathQuery(summaryQuery) as Array<TFHppleElement>
return summary.map { $0.firstChild.content }
Asynchroner NSURLRequest:

Code:
func getData(completionHandler : (_: NSURLResponse!, _: NSData!, _: NSError!) -> ()) {
  
        NSURLConnection.sendAsynchronousRequest(self.request, queue: self.queue, completionHandler: completionHandler)
}
Verwendung von Enums:

Code:
enum SampleEnum : Int {
    
    case Internal = 0
    case External
    
    var description : String {
    
    switch self {
    case .Internal:
        return "EnumValue A"
    case .External:
        return "EnumValue B"
        }
    }
}
die description-Variable ist z.B. ganz praktisch, um Informationen in einem UITableView anzuzeigen:

Code:
override func tableView(_: UITableView!, titleForHeaderInSection section: Int) -> String! {
        
    return SampleEnum.fromRaw(section)!.description
}
In Verbindung mit einem konstanten Wert für numberOfSectionsInTableView kann man fromRaw() ruhig entpacken.

didSet für Properties in einer UITableViewCell:

Code:
class NDTableViewCell: UITableViewCell {

    var item : NDItem? {
    
    didSet {
     
        if let item = self.item {
            
            titleLabel.text = "\(item.title) blabla"
            detailLabel.text = "\(item.location), \(item.type.toRaw())"
        }
    }
    }
    
    @IBOutlet var titleLabel : UILabel
    @IBOutlet var detailLabel : UILabel
}
Die Arche Noah:

Code:
let 🌍 = "🐶🐺🐱🐭🐹🐰🐸🐯🐨🐻🐮🐗🐵🐒🐴🐑🐘🐼🐧🐦🐤🐥🐣🐔🐍🐢🐠🐳"
let 🚢 = Array<Character>(🌍).map{ 💕 in 💕 + 💕 }
🚢
Verwendung von Tuples:

Code:
let (title, value) = getData()
println(title)
alternativ:

Code:
let tuple = getData()
println(tuple.0)
Neuer Code wird nach und nach hinzugefügt.
snow is offline  
Thanks
5 Users
Old 06/20/2014, 15:02   #2


 
.Marcel''s Avatar
 
elite*gold: 100
Join Date: Sep 2009
Posts: 8,136
Received Thanks: 2,760
Beschäftige mich auch mit swift, sehr nett von dir
.Marcel' is offline  
Reply


Similar Threads Similar Threads
[Selling] Swift Zulian Tiger, Swift Razzashi Raptor, Sea Turtle.
08/04/2013 - World of Warcraft Trading - 0 Replies
This account has only one level 90 - a Tauren Warrior on Al'akir EU (http://www.maskedarmory.com/anonymous-wow-armory- profile-217491.html) As the title says, the account has 3 very rare mounts, two of which will be forever unobtainable. Respond on here or add me on skype (fire.stez) to talk further. I'll add that the account also has Diablo 3 on it. I don't know if anyone even plays that anymore but it's worth a mention.
Swift Frostsaber to Swift Spectral Tiger
02/06/2010 - World of Warcraft - 5 Replies
Hat die mpq jemand oder könnte sie vielleicht schnell machen? Gibt n fettes thx
Request: Swift Brown Steed into Swift Brewfest Kodo
09/20/2008 - World of Warcraft - 0 Replies
Ich suche jemanden, der mir Swift Brown Steed in das Swift Brewfest Kodo machen kann. PM



All times are GMT +2. The time now is 22:58.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.