Swift Extensions

Swift Extensions

Written By: Debasis Das (30-Mar-2015)

Extensions add new functionality to an existing class, structure, or enumeration type.

This includes the ability to extend types for which we do not have access to the original source code (known as retroactive modeling).

Extensions in Swift are similar to categories in Objective-C. (Unlike Objective-C categories, Swift extensions do not have names.)

  • Extensions in Swift can:
    • Add computed properties and computed static properties
    • Define instance methods and type methods
    • Provide new initializers
    • Define subscripts
    • Define and use new nested types
    • Make an existing type conform to a protocol

Sample Swift Extension.

In this sample we will create two swift extensions (NSColor extension & NSString Extension)

In the NSColor extension we will add a new color called as lightRedColor which returns a NSColor instance and in NSString we will add a method called as isValidEmailAddress that returns a boolean value (true/false)

NSColor & NSString Swift Extensions

//  Extensions.swift
//  SwiftExtension
//  Created by Debasis Das on 3/30/15.
//  Copyright (c) 2015 Knowstack. All rights reserved.

import Cocoa

extension NSColor {
    
    private struct SharedColors
    {
        static let completeItemTextColor = NSColor(red: 0.70, green: 0.70, blue: 0.70, alpha: 1)
    }

    public class func completeItemTextColor() -> NSColor
    {
        return SharedColors.completeItemTextColor
    }

    
    public class func lightRedColor()-> NSColor
    {
        return NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 0.5)
    }
    
    
}

extension NSString {
    
    //Valid Email Address Check in Swift
    public class func isValidEmailAddress(emailID: String) -> Bool
    {
        let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
        if let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx) {
            return emailTest.evaluateWithObject(emailID)
        }
        return false
    }
}

//Test NSColor Extension

        println(NSColor.lightRedColor()) //NSCustomColorSpace sRGB IEC61966-2.1 colorspace 1 0 0 0.5

//Test NSString Extension

println(NSString.isValidEmailAddress(“ddas@knowstack.com”)) //true      println(NSString.isValidEmailAddress(“d_das@knowstack.com@gmail.com”)) //false      println(NSString.isValidEmailAddress(“knowstack.com@gmail.com”)) //true      println(NSString.isValidEmailAddress(“213213213213”)) //false

Reference

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Extensions.html

Posted in Swift Tagged with: , , , , ,
0 comments on “Swift Extensions
1 Pings/Trackbacks for "Swift Extensions"

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts


Hit Counter provided by technology news