Skip to content

Instantly share code, notes, and snippets.

// example for http://stackoverflow.com/a/26261978/1271826
// create scroll view
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[scrollViewSuperView addSubview:scrollView];
// create container 1
@robertmryan
robertmryan / gist:1ecf5c7f4bf4d78a8cbc
Last active August 29, 2015 14:13
An example of populating a HTTPBody of NSMutableURLRequest for `application/x-www-form-urlencoded` request in response to http://stackoverflow.com/questions/28008874/post-with-swift-and-api/28009796#comment44430194_28009796
extension NSMutableURLRequest {
/// Populate the HTTPBody of `application/x-www-form-urlencoded` request
///
/// :param: contentMap A dictionary of keys and values to be added to the request
func setBodyContent(contentMap: [String : String]) {
let parameters = map(contentMap) { (key, value) -> String in
return "\(key)=\(value.stringByAddingPercentEscapesForQueryValue()!)"
}
@robertmryan
robertmryan / gist:c526b6a4e477428f2a15
Last active February 28, 2016 23:21
Lazily instantiate location manager
lazy var locationManager: CLLocationManager = {
let manager = CLLocationManager()
manager.requestWhenInUseAuthorization()
return manager
}()
var thing = 0
func hi() {
// Do something
thing++
print(thing)
}
@IBAction func somethingHi(sender: AnyObject) {
hi()
@robertmryan
robertmryan / gist:2e2418e9bb9a3cb37181
Last active February 28, 2016 23:20
More complete example
var text: String!
override func viewDidLoad() {
super.viewDidLoad()
let url = NSBundle.mainBundle().URLForResource("test", withExtension: "json")!
var files = [String : NSFileWrapper]()
var error: NSError?
files["file.json"] = NSFileWrapper(URL: url, options: nil, error: &error)
func myFunction() -> NSArray {
var array: NSArray
Synchronized(someObject) {
// Stuff to do.
// don't "return" the array, here, but just set that local var above
}
return array // you can only "return" the `NSArray` object from outside the `Synchronized` closure
}
// original class
class A {
var x: String
init(x: String) {
self.x = x
}
}
// new class
@robertmryan
robertmryan / gist:8af2c98902e39a60e177
Last active February 28, 2016 23:23
Resize views in `viewWillLayoutSubviews`
class ViewController: UIViewController {
var redView: UIView!
var greenView: UIView!
var blueView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
redView = UIView()
@robertmryan
robertmryan / gist:5ca3ef29330086e25675
Last active February 28, 2016 23:23
Range rendition of `splitEvery` in Swift 2
extension Range {
/// Split range into an array of ranges of predetermined size.
///
/// Adapted from http://stackoverflow.com/a/26691258/1271826
///
/// - parameter every: How many items per subrange
/// - returns: An array of Range objects
func splitEvery(every: Element.Distance) -> [Range] {
@IBAction func calculateButton(sender: AnyObject) {
if let value = Int(number.text!) {
var isPrime = true
if value == 1 {
isPrime = false
}
for var i = 2; i < value; i++ {
if value % i == 0 {