Posts

Swift: Understanding Mutating Functions

 classes are reference type whereas structures and enumerations are value types. The properties of value types cannot be modified within its instance methods by default. In order to modify the properties of a value type, you have to use the mutating keyword in the instance method. With this keyword, your method can then have the ability to mutate the values of the properties and write it back to the original structure when the method implementation ends. Below is a simple implementation of Stack in Swift that illustrates the use of mutating functions.     struct Stack { public private ( set ) var items = [ Int ] ( ) // Empty items array mutating func push ( _ item : Int ) { items . append ( item ) } mutating func pop ( ) - > Int ? { if ! items . isEmpty { return items . removeLast ( ) } return nil } } var stack = Stack ( ) stack . push ( 4 ) stack . push ( 78 ) stack . items // [4, 7

Reverting all Xcode settings to default settings

This is the officially recommended way to delta Xcode 5's preferences, type in Terminal.app: defaults delete com . apple . dt . Xcode   That should restore Xcode to the state of its first launch. (for older versions of Xcode the command was defaults delete com.apple.Xcode , i.e. without the dt in the middle).

Creating Custom URL schemes in iOS/iPhone

Image
iPhone apps can also specify their own custom URL scheme (for example, myapp://doStuff). When might you want to use a custom URL scheme for your app? To transfer data from your app to your  another app To allow other apps (or even web pages) to call your app (and send data to it) To handle callbacks for custom authentication (such as OAuth ) and third party API's Implementing a Custom URL Scheme   The first step is to create a custom URL scheme – start by locating and clicking on the project info.plist in the Xcode Project Navigator. With the plist displayed in the right pane, right click on the list and select Add Row : From the list presented scroll down and select URL types .  Select URL Types for the new item. Once that's added, click the grey arrow next to "URL Types" to show "Item 0". Set your URL identifier to a unique string - something like com.yourcompany.yourappname.    After you've set the URL identifier, sele

Add shadow to text in UITextView

Image
    NSShadow * shadow = [[NSShadow alloc] init];     shadow.shadowColor = [UIColor blackColor];     shadow.shadowOffset = CGSizeMake(1, 2);        NSDictionary * textAttributes =  @{                                        NSForegroundColorAttributeName : [UIColor blueColor],                                        NSShadowAttributeName          : shadow,                                        NSFontAttributeName            : [UIFont boldSystemFontOfSize:20]                                        };        txtView.attributedText = [[NSAttributedString alloc] initWithString:@"Go to Heaven for the climate, Hell for the company." attributes:textAttributes];

Disable iPhoto auto launch when connecting an iPhone / iPad

Image
Plug in your iPhone/iPad Open application "Image capture" Select your iPhone/iPad device Press the triangle in square symbol in the lower left corner. Choose "No application" in the drop-list under "Connecting this iPhone opens:"  

add small image after text in UILabel

NSTextAttachment * attachment = [[ NSTextAttachment alloc ] init ]; attachment . image = [ UIImage imageNamed :@ "myimage.png" ]; NSAttributedString * attachmentString = [ NSAttributedString attributedStringWithAttachment : attachment ]; NSMutableAttributedString * myString = [[ NSMutableAttributedString alloc ] initWithString :@ "My label text" ]; [ myString appendAttributedString : attachmentString ]; myLabel . attributedText = myString ;

Struck through text in UILabel

In iOS 6.0 and up UILabel supports NSAttributedString NSMutableAttributedString * attributeString = [[ NSMutableAttributedString alloc ] initWithString :@ "Your String here" ]; [ attributeString addAttribute : NSStrikethroughStyleAttributeName value :@ 2 range : NSMakeRange ( 0 , [ attributeString length ])]; Definition : - ( void ) addAttribute :( NSString *) name value :( id ) value range :( NSRange ) aRange Parameters List: name : A string specifying the attribute name. Attribute keys can be supplied by another framework or can be custom ones you define. For information about where to find the system-supplied attribute keys, see the overview section in NSAttributedString Class Reference. value : The attribute value associated with name. aRange : The range of characters to which the specified attribute/value pair applies. Then yourLabel . attributedText = attributeString ;