let greeting = "Guten Tag!"
greeting[greeting.startIndex]
// G
greeting[greeting.endIndex.predecessor()]
// !
greeting[greeting.startIndex.successor()]
// u
let index = greeting.startIndex.advancedBy(7)
greeting[index]
// a
var str = "Hello, playground"
for chr in str.characters.reverse() {
print(chr)
}
Set
, вместо Array
и старался бы избегать написания велосипедов.var uniqueStringSet = Set<String>()
uniqueStringSet.unionInPlace(["Vasa", "Petya", "Gena"])
uniqueStringSet.unionInPlace(["Sasha", "Nikita", "Petya", "Serega", "Vasa"])
print(uniqueStringSet)
let array = Array<String>(uniqueStringSet)
var newArray = array.map{Optional($0)}
newArray.insert(nil, atIndex: 0)
print(newArray)
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
...
let userDefaults = NSUserDefaults.standardUserDefaults()
if var numberOfLaunches = userDefaults.objectForKey("numberOfLaunches") as? Int {
numberOfLaunches++
userDefaults.setInteger(numberOfLaunches, forKey: "numberOfLaunches")
} else {
let n = 1
userDefaults.setInteger(n, forKey: "numberOfLaunches")
}
print(userDefaults.objectForKey("numberOfLaunches") as! Int)
return true
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedIndexPath = tableView.indexPathForSelectedRow
tableView.beginUpdates()
tableView.endUpdates()
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if selectedIndexPath == indexPath {
return 107
} else {
return 50
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let identifier = segue.identifier {
if identifier == "SideBarTVC" {
let vc = segue.destinationViewController
vc.delegate = self
}
}
}
let dateFormatter = NSDateFormatter()
dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle
let gmtTimeZone = NSTimeZone(abbreviation: "GMT")
dateFormatter.timeZone = gmtTimeZone
dateFormatter.dateFormat = "yyyyMMdd"
var dates = [String]()
let threeMonthAgoComponent = NSDateComponents()
threeMonthAgoComponent.month = -3
var date = NSDate()
let calendar = NSCalendar.currentCalendar()
let lastDate = calendar.dateByAddingComponents(threeMonthAgoComponent, toDate: date, options: NSCalendarOptions(rawValue: 0))
let dayAgoComponent = NSDateComponents()
dayAgoComponent.day = -1
while (date != lastDate) {
date = calendar.dateByAddingComponents(dayAgoComponent, toDate: date, options: NSCalendarOptions.MatchFirst)!
dates.append(dateFormatter.stringFromDate(date))
}
print(dates)
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let lastRow = indexPath.row
if lastRow == objects.count - 1 {
fetchData(lastRow)
}
}
private func fetchData(lastRow: Int) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * Int64(NSEC_PER_SEC)), dispatch_get_main_queue()) { () -> Void in
let object = "New data"
self.objects.append(object)
self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: lastRow + 1, inSection: 0)], withRowAnimation: .Automatic)
}
}