Skip to content

Instantly share code, notes, and snippets.

@sunapi386
Created January 13, 2016 22:21
Show Gist options
  • Save sunapi386/d281c7632ac726715982 to your computer and use it in GitHub Desktop.
Save sunapi386/d281c7632ac726715982 to your computer and use it in GitHub Desktop.
Swift gists
// Generic Types
class Carousel<T: CustomStringConvertible> {
var items = [T]()
var currentPosition = 0
var count: Int {return items.count}
var isEmpty: Bool {return items.isEmpty}
func append(item: T) {items.append(item)}
func next() -> T? {
if self.isEmpty { return nil }
let nextItem = items[currentPosition]
currentPosition = (currentPosition + 1) % count
return nextItem
}
subscript(position: Int) -> T? {
if self.isEmpty { return nil }
return items[position % count]
}
}
var intCarousel = Carousel<Int>()
intCarousel.append(3)
intCarousel.append(5)
extension Carousel: CustomStringConvertible {
public var description: String {
return "\(self.dynamicType) has \(count) items: " +
items.map{return $0.description}.joinWithSeparator(",")
}
}
print(intCarousel)
// When you don't own the class, subclass it.
class Carousel<T> {
var items = [T]()
var currentPosition = 0
var count: Int {return items.count}
var isEmpty: Bool {return items.isEmpty}
func append(item: T) {items.append(item)}
func next() -> T? {
if self.isEmpty { return nil }
let nextItem = items[currentPosition]
currentPosition = (currentPosition + 1) % count
return nextItem
}
subscript(position: Int) -> T? {
if self.isEmpty { return nil }
return items[position % count]
}
}
class PrintableCarousel<T: CustomStringConvertible>: Carousel<T>, CustomStringConvertible {
var description: String {
var d = "\(self.dynamicType) with \(count) items: "
for it in items {
d += ",\(it.description)"
print(it.description)
}
return d
}
}
var intCarousel = Carousel<Int>()
intCarousel.append(3)
intCarousel.append(5)
print(intCarousel)
// Associated Types
protocol Traversable {
typealias Element
var count: Int {get}
var first: Element? {get}
var last: Element? {get}
mutating func previous() -> Element?
mutating func next() -> Element?
mutating func reset()
}
extension Carousel: Traversable {
public var first: T? {return self.isEmpty ? nil : self[0]}
public var last: T? {return self.isEmpty ? nil : self[self.count - 1]}
public func previous() -> T? {
if self.isEmpty {
return nil
}
currentPosition = (currentPosition + count - 1) % self.count
return self[currentPosition]
}
public func reset() {
currentPosition = 0
}
}
import Foundation
func weatherDataForCity(city: String, closure: (json: NSDictionary) -> ()) {
print("weatherDataForCity")
var urlString = "http://api.openweathermap.org/data/2.5/weather?q=" + city
var url = NSURL(string: urlString)
if url == nil {
print("dead")
return
}
print("sdsd")
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) in
print("sss")
do {
print("task")
let json = try NSJSONSerialization.JSONObjectWithData(data!,
options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
print("json \(json)")
// closure(json: json)
} catch {
print("shit")
}
}
print("e")
task.resume()
print("d")
}
//Sample usage
weatherDataForCity("Chicago") {(json) in
print(json)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment