Xcode Snippets

August 27, 2018

Snippets

Web Dev

These are just raw notes on Xcode. They aren’t necessarily organized in any specific way, this is more of a board to place things I learn about the program. I used to have these unorganized within the notes app on my phone, but by placing them here I hope that someone else can use them too. If you have any questions on the notes or a question on a particular snippet feel free to reach out to me using the form at the bottom of this page.

For Haptic Feedback

Put the following in appDelegate.swift for global use:

let impact = UIImpactFeedbackGenerator()
impact.impactOccurred()

To open a viewcontroller in swift:

Create a segue by right clicking and dragging to the appropriate view controller and then on button click add:

performSegue(withIdentifier: "Segueidentifier", sender: self)} 

To find and replace in a NSString:

NSString * oldMPG = [MPGValue stringByReplacingOccurrencesOfString:@"MPG" withString:@""];

To link a .xib to a class:

  1. Open your .xib.
  2. In placeholder: select File’s Owner.
  3. In the third thumbnail:”Identity Inspector” of the right panel, edit the “Custom Class” field and enter the name of your class.

I also then had to:

  1. Click the root view under Objects.
  2. Click the last tab: “Show Connections”.
  3. Drag “New referencing outlet” to “File’s Owner” and select “view”.

To add border color to an element:

Add this to the .m file:

@implementation CALayer(INSERT NORMAL IMPLEMENTTION NAME HERE)
-(void)setBorderUIColor:(UIColor*)color {
   self.borderColor = color.CGColor;
}
-(UIColor*)borderUIColor {
   return [UIColor colorWithCGColor:self.borderColor];
}
@end

Add this to the .h file:

#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
@property(nonatomic, assign) UIColor* borderUIColor;

then add this to the xib file:

@joekotlan on Twitter