Skip to content

Instantly share code, notes, and snippets.

@chenr2
chenr2 / tabBarColor.swift
Created July 16, 2015 11:38
Swift: How to set the tab bar color
tabBarController?.tabBar.selectedImageTintColor = .redColor()
@chenr2
chenr2 / suppressCallout.swift
Last active August 29, 2015 14:25
Swift: How to suppress callout for user location
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView!{
// user location
if annotation is MKUserLocation {
(annotation as! MKUserLocation).title = ""
// use blue pulsy instead of pin
return nil
}
// other pins
return mapView.dequeueReusableAnnotationViewWithIdentifier("pin") as? MKPinAnnotationView
}
@chenr2
chenr2 / dispatch.swift
Created July 16, 2015 11:46
Swift snippet: hop back to the main thread
dispatch_async(dispatch_get_main_queue()) {
self.someMethod(self.someProperty)
}
@chenr2
chenr2 / alertview.md
Last active August 29, 2015 14:25
Swift UIAlertView for iOS 7 compatibility. Also demonstrates adding a TextField to the AlertView.

Using UIAlertView

UIAlertView is deprecated, so use it for iOS 7 compatibility. Otherwise, use UIAlertController.

let alertView = UIAlertView()
alertView.message = "here"
alertView.addButtonWithTitle("Ok")
alertView.show()
@chenr2
chenr2 / stringify.md
Last active August 29, 2015 14:25
Swift: JSON stringify

How to stringify JSON

This assumes you have SwiftyJSON.

Start with a Dictionary

let paramsDictionary = [
    "title": "foo",
 "description": "bar"
@chenr2
chenr2 / singleton.md
Last active August 29, 2015 14:25
Swift: singleton
@chenr2
chenr2 / equatable.md
Last active August 29, 2015 14:25
Swift: equatable protocol

How to tell if two things are the same

You want to iterate over an array of custom objects, and need to tell whether two objects are equal. You're not concerned with two objects being identical in memory, but equivalent based on your own definition.

class Student {
    var studentId = 0
    var firstName = "Bob"
}
@chenr2
chenr2 / stringCount.md
Created July 18, 2015 01:15
Swift: Count

How to count characters in a string

Just use count

count(textView.text!)

Error when performing segue on viewDidLoad

Getting this error message:

Presenting view controllers on detached view controllers is discouraged

This is happening because you’re not supposed to perform a segue until viewDidAppear