When I first saw the swift extension, I thought about the extensions and categories of OC, so I was looking for Swift's expansion and OC extensions and categories. After several searches, the expansion of swift and the category of OC were found to be the same. But I'm a little more than a piece of the way to find out what it is like for the OC extension of Swift. But I was still not found, so I was quiet to think about the expansion of OC and classification of the role and difference is what. What the usage scenario is. So, let's look at the OC categories and extensions first:
Category: You can dynamically add new methods to existing classes
The interface file
@interface the existing class (class name)
//method definition
@end
//Implementation Section
@implementation the existing class (class name)
//method implementation
@end
Eg:
#import <UIKit/UIKit.h>
@interface uicolor (UIColorUInt32)
+ (Uicolor *) Colorwithhexstring: ( NSString *) Valuestr;
@end
#import "uicolor+uicoloruint32.h"
@implementation uicolor (UIColorUInt32)
+ (Uicolor *) Colorwithhexstring: (NSString *) valuestr{
Nsscanner * scanner = [Nsscanner scannerwithstring:valuestr];
UInt32 hexstr = 0;
if (![ Scanner Scanhexint:&hexstr]) {
return [Uicolor Clearcolor];
} else{
return [Uicolor colorwithred: (cgfloat) ((hexstr&0xff0000) >>16)/255.0
Green: (CGFloat) (( HEXSTR&0X00FF00) >>8)/255.0
Blue: (CGFloat) ((HEXSTR&0X0000FF))/255.0
Alpha: (cgfloat) (1.0 )];
}
}
@end
Extension: Just extend the interface of a class, which we use more, generally we are accustomed to adding extensions in. m files. It then defines some of the interfaces and properties that are private and do not want to be accessed externally.
@interface already have classes () < proxies >
//define variables
//define methods
@end
eg:
#import <Foundation/Foundation.h>
@interface person:nsobject
@property (nonatomic, copy) NSString * name;
-(NSString *) getString;
@end
#import "Person.h"
@interface person ()
@property (nonatomic, assign) Nsinteger age;
-(void) walk;
@end
@implementation Person
-(void) walk{
NSLog (@ "walk ...");
-(NSString *) getstring{
return _name;
}
@end
OK, here, I believe I feel a little bit, Swift doesn't need anything like OC extensions at all. Because Swift has only one file. Everything in it is within class. So, after we've removed the stem, let's take a look at the swift extension.
Like OC, we choose Ios-->source-->swift file to create a Swift document.
Import Foundation
import UIKit
extension uicolor{
convenience init (valuestr:string) {Let
scanner: Nsscanner = Nsscanner (string:valuestr)
var valuergb:uint32 = 0
if scanner.scanhexint (&valuergb) = = False { C6/>self.init (red:0,green:0,blue:0,alpha:0)
}else{
self.init (
red:cgfloat (Valuergb & 0xFF0000) >>16)/255.0,
green:cgfloat ((Valuergb & 0x00FF00) >>8)/255.0,
blue:cgfloat (Valuergb & 0x0000FF)/255.0,
alpha:cgfloat (1.0)}}}//
* Explain here:
Nsscanner: This class can scan a string, To pick out the characters we need, Method Scanhexint (&VALUERGB), you can scan scanner's 16-binary data and convert it to 10-binary data to copy to Valuergb.
convenience: Facilitates initialization of keywords.
*
//use:
Self.view.backgroundColor = Uicolor (valuestr: "FFFFFF");
Do not generate a swift file in addition:
/* 1, after deleting a bridging-header and header file, it should be generation objective-c in building Setting, Swift Compilier-code bridging The header removes bridge 2, extension: extensions, 3, subscript: Subscript script: A syntax for indexing, quickly fetching values for a thing, such as an array a[0]. In swift, we can define subscript scripts for classes, structs, and enumerations. The following banner method uses set, get to define the read and write properties, no two are required; When defining a set property, the passed-in parameter is named NewValue, 4, Enumerate (): The character comes with a function, and returns index and data */import UIKit Ext Ension string{Subscript (start:int,length:int)->string{get{return (self as nsstring). Substrin Gwithrange (Nsrange (location:start,length:length))} set{let tmp = self var s =
"" var e = "" for (Idx,item) in Tmp.characters.enumerate () {if idx<start{
s + = "\ (item)"} if idx>=start+length{e + = "\ (item)"
}} self = s+newvalue+e}} subscript (Index:int)->string{ get{return String (Self[self.startindex.advancedby (Index)])} set{let tmp = self self = "" For (idx, item) in Tmp.characters.enumerate () {if idx = = index{Self + = "\ (newvalue) "}else{self + =" \ (item) "}}}}} class Vi Ewcontroller:uiviewcontroller {override Func Viewdidload () {super.viewdidload () var str = "FCF"// Print ("\ (str[4,2])")//Cross print ("\ (str[2,1])") str[3,3] = "eng" Print ("\ (str)")}}
OK, the two examples I believe are enough to understand and use.