Skip to content

Instantly share code, notes, and snippets.

// Playground - noun: a place where people can play
import Cocoa
/* Classes and Structures
Traditionally, we've referred to instances of a class as "objects" however, because Swift classes and structures are so much alike, it's preferable to use the more general term -- 'Instance'
Even though these two Types have a lot of similarity, they are handled by the system very differently. ***Structures are values which are always copied when they are passed around, whereas Classes use reference counting.*** This means more than one variable can reference a specific class instance, but you will never have multiple references to the same specific instance of a Struct
@loganwright
loganwright / Classes
Last active August 29, 2015 14:02
Classes
// Playground - noun: a place where people can play
import Cocoa
// Just a restatement for myself because it applies to Classes and Structures -- No need to read this part.
/* Classes and Structures
Traditionally, we've referred to instances of a class as "objects" however, because Swift classes and structures are so much alike, it's preferable to use the more general term -- 'Instance'
@loganwright
loganwright / Closures
Last active August 29, 2015 14:02
Closures
// Playground - noun: a place where people can play
import Cocoa
/*
"Closures are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages."
If you've already worked with functions in Swift, closures should look relatively familiar to you because functions are a type of named closure. If you haven't looked at functions yet, I highly recommend doing that.
*/
@loganwright
loganwright / UIBezierPathSquiggle
Created June 14, 2014 16:48
Squiggle UIBezierPath
- (void) drawSquiggleInRect:(CGRect)rect {
// Assure positive values
rect = CGRectStandardize(rect);
// Find anchor points
CGPoint anchorLowerLeft = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height);
CGPoint anchorTopRight = CGPointMake(rect.origin.x + rect.size.width, rect.origin.y);
// 10.0 works pretty good!
// Playground - noun: a place where people can play
import Cocoa
/*
One of the new features introduced in Swift is the ability to easily add operator support to custom classes and structs. Don't worry if that doesn't make sense right away, we'll talk more about it now.
Let's start by declaring a custom struct.
*/

Swift is great, but there's a lot of useful libraries that still exist in ObjC that we want to be able to use alongside our swift projects. Let's look at how to use Objective-C code in a Swift project!

Let's start a new project. A single view project is fine for what we're doing, just make sure the language is set to Swift. Once a project is created, I'm personally a big fan of running it before I add anything just to make sure everything is configured properly.

Ok, everything is working fine, let's move right in and add an Objective-C class. When you click 'New File' you'll see a new option with Xcode 6 called 'Cocoa Class'. Click that. Once the window is up, let's name our class 'ColorfulLabel', make it a subclass of UILabel, and make sure that our language is set to Objective-C. This way Xcode will generate our .h and .m file simultaneously. Click "Next" and then "Create" our new class.

You should be hit with a prompt that says "Would you like to configure an Objective-C bridging

@loganwright
loganwright / SkipParse-Login
Created July 20, 2014 15:50
SkipParse - Login
#Step 1 - General Setup
- Parse Acct
- Parse Proj in Xcode / Parse Dashboard
- Add keys
Parse's docs are really good for general setup, if you want to run through it, that'd probably be fine. Once you get to importing Swift in AppDelegate.m
@loganwright
loganwright / DateKeys.h
Created August 20, 2014 14:05
Date Keys
//
// DateKeys.h
// VoxNotes
//
// Created by Logan Wright on 5/6/14.
// Copyright (c) 2014 Logan Wright. All rights reserved.
//
#import <Foundation/Foundation.h>
@loganwright
loganwright / Readme.md
Last active August 4, 2023 03:50
UIView Gesture Recognizer Extension For Swift

Interface for dealing with gesture recognizers via native swift closure syntax

import UIKit

class ViewController: UIViewController {
                            
    override func viewDidLoad() {
        super.viewDidLoad()
 view.addSingleTapGestureRecognizerWithResponder { (tap) -&gt; Void in
@loganwright
loganwright / SwiftLessonOne_Revamp.md
Last active August 29, 2015 14:06
SwiftLessonOne_Revamp

#Introduction to Variables

To declare a variable in Swift, simply assign something to it, like below:

import Cocoa

var str = "Hello, playground"

As you can see, there is no explicit type declared. This is because type inference will notice that the variable str is being assigned a string and the compiler will infer that str is of type String