diff --git a/R.swift.podspec b/R.swift.podspec index ff6592fc..e48be909 100644 --- a/R.swift.podspec +++ b/R.swift.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "R.swift" - s.version = "0.8.1" + s.version = "0.8.2" s.summary = "Use strong typed, autocompleted resources like images and segues in Swift" s.description = <<-DESC @@ -22,7 +22,7 @@ Pod::Spec.new do |s| s.platform = :ios, "7.0" - s.source = { :http => "https://github.com/mac-cain13/R.swift/releases/download/v0.8.1/rswift-0.8.1.zip" } + s.source = { :http => "https://github.com/mac-cain13/R.swift/releases/download/v0.8.2/rswift-0.8.2.zip" } s.preserve_paths = "rswift" diff --git a/R.swift/func.swift b/R.swift/func.swift index d274b11a..5c6fd68d 100644 --- a/R.swift/func.swift +++ b/R.swift/func.swift @@ -125,7 +125,7 @@ func storyboardStructForStoryboard(storyboard: Storyboard) -> Struct { let validateImagesLines = distinct(storyboard.usedImageIdentifiers) .map { "assert(UIImage(named: \"\($0)\") != nil, \"[R.swift] Image named '\($0)' is used in storyboard '\(storyboard.name)', but couldn't be loaded.\")" } - let validateImagesFunc = Function(isStatic: true, name: "validateImages", parameters: [], returnType: Type._Void, body: join("\n", validateImagesLines)) + let validateImagesFunc = Function(isStatic: true, name: "validateImages", generics: nil, parameters: [], returnType: Type._Void, body: join("\n", validateImagesLines)) let validateViewControllersLines = catOptionals(storyboard.viewControllers .map { vc in @@ -133,7 +133,7 @@ func storyboardStructForStoryboard(storyboard: Storyboard) -> Struct { "assert(\(sanitizedSwiftName($0)) != nil, \"[R.swift] ViewController with identifier '\(sanitizedSwiftName($0))' could not be loaded from storyboard '\(storyboard.name)' as '\(vc.type)'.\")" } }) - let validateViewControllersFunc = Function(isStatic: true, name: "validateViewControllers", parameters: [], returnType: Type._Void, body: join("\n", validateViewControllersLines)) + let validateViewControllersFunc = Function(isStatic: true, name: "validateViewControllers", generics: nil, parameters: [], returnType: Type._Void, body: join("\n", validateViewControllersLines)) return Struct(type: Type(name: sanitizedSwiftName(storyboard.name)), lets: [], vars: instanceVars + initialViewControllerVar + viewControllerVars, functions: [validateImagesFunc, validateViewControllersFunc], structs: []) } @@ -168,6 +168,7 @@ func nibStructForNib(nib: Nib) -> Struct { let instantiateFunc = Function( isStatic: false, name: "instantiateWithOwner", + generics: nil, parameters: instantiateParameters, returnType: Type(name: "[AnyObject]"), body: "return instance.instantiateWithOwner(ownerOrNil, options: optionsOrNil)" @@ -179,9 +180,10 @@ func nibStructForNib(nib: Nib) -> Struct { Function( isStatic: false, name: "\($0.ordinal.word)View", + generics: nil, parameters: instantiateParameters, returnType: $0.view.asOptional(), - body: "return \(instantiateFunc.swiftName)(ownerOrNil, options: optionsOrNil)[\($0.ordinal.number - 1)] as? \($0.view)" + body: "return \(instantiateFunc.callName)(ownerOrNil, options: optionsOrNil)[\($0.ordinal.number - 1)] as? \($0.view)" ) } @@ -231,7 +233,7 @@ func varFromReusable(reusable: Reusable) -> Var { // Validation func validateAllFunctionWithStoryboards(storyboards: [Storyboard]) -> Function { - return Function(isStatic: true, name: "validate", parameters: [], returnType: Type._Void, body: join("\n", storyboards.map(swiftCallStoryboardValidators))) + return Function(isStatic: true, name: "validate", generics: nil, parameters: [], returnType: Type._Void, body: join("\n", storyboards.map(swiftCallStoryboardValidators))) } func swiftCallStoryboardValidators(storyboard: Storyboard) -> String { diff --git a/R.swift/types.swift b/R.swift/types.swift index ce5cc361..2d1c1b70 100644 --- a/R.swift/types.swift +++ b/R.swift/types.swift @@ -139,19 +139,21 @@ struct Let: Printable { struct Function: Printable { let isStatic: Bool let name: String + let generics: String? let parameters: [Parameter] let returnType: Type let body: String - var swiftName: String { + var callName: String { return sanitizedSwiftName(name, lowercaseFirstCharacter: true) } var description: String { let staticString = isStatic ? "static " : "" + let genericsString = generics.map { "<\($0)>" } ?? "" let parameterString = join(", ", parameters) let returnString = Type._Void == returnType ? "" : " -> \(returnType)" - return "\(staticString)func \(swiftName)(\(parameterString))\(returnString) {\n\(indent(body))\n}" + return "\(staticString)func \(callName)\(genericsString)(\(parameterString))\(returnString) {\n\(indent(body))\n}" } struct Parameter: Printable { diff --git a/R.swift/values.swift b/R.swift/values.swift index ae53922d..e6906976 100644 --- a/R.swift/values.swift +++ b/R.swift/values.swift @@ -61,7 +61,8 @@ let ReuseIdentifierUITableViewExtension = Extension( functions: [ Function( isStatic: false, - name: "dequeueReusableCellWithIdentifier", + name: "dequeueReusableCellWithIdentifier", + generics: "T : \(Type._UITableViewCell)", parameters: [ Function.Parameter(name: "identifier", type: ReuseIdentifier.type), Function.Parameter(name: "forIndexPath", localName: "indexPath", type: Type._NSIndexPath.asOptional(), defaultValue: "nil") @@ -72,7 +73,8 @@ let ReuseIdentifierUITableViewExtension = Extension( Function( isStatic: false, - name: "dequeueReusableCellWithIdentifier", + name: "dequeueReusableCellWithIdentifier", + generics: "T : \(Type._UITableViewCell)", parameters: [ Function.Parameter(name: "identifier", type: ReuseIdentifier.type), ], @@ -82,7 +84,8 @@ let ReuseIdentifierUITableViewExtension = Extension( Function( isStatic: false, - name: "dequeueReusableHeaderFooterViewWithIdentifier", + name: "dequeueReusableHeaderFooterViewWithIdentifier", + generics: "T : \(Type._UITableViewHeaderFooterView)", parameters: [ Function.Parameter(name: "identifier", type: ReuseIdentifier.type), ], @@ -92,7 +95,8 @@ let ReuseIdentifierUITableViewExtension = Extension( Function( isStatic: false, - name: "registerNib", + name: "registerNib", + generics: "T: \(NibResourceProtocol.type) where T: \(ReusableProtocol.type), T.T: UITableViewCell", parameters: [ Function.Parameter(name: "nibResource", type: Type(name: "T")) ], @@ -102,7 +106,8 @@ let ReuseIdentifierUITableViewExtension = Extension( Function( isStatic: false, - name: "registerNibs", + name: "registerNibs", + generics: "T: \(NibResourceProtocol.type) where T: \(ReusableProtocol.type), T.T: UITableViewCell", parameters: [ Function.Parameter(name: "nibResources", type: Type(name: "[T]")) ], @@ -112,7 +117,8 @@ let ReuseIdentifierUITableViewExtension = Extension( Function( isStatic: false, - name: "registerNibForHeaderFooterView", + name: "registerNibForHeaderFooterView", + generics: "T: \(NibResourceProtocol.type) where T: \(ReusableProtocol.type), T.T: UIView", parameters: [ Function.Parameter(name: "nibResource", type: Type(name: "T")) ], @@ -127,7 +133,8 @@ let ReuseIdentifierUICollectionViewExtension = Extension( functions: [ Function( isStatic: false, - name: "dequeueReusableCellWithReuseIdentifier", + name: "dequeueReusableCellWithReuseIdentifier", + generics: "T: \(Type._UICollectionViewCell)", parameters: [ Function.Parameter(name: "identifier", type: ReuseIdentifier.type), Function.Parameter(name: "forIndexPath", localName: "indexPath", type: Type._NSIndexPath) @@ -138,7 +145,8 @@ let ReuseIdentifierUICollectionViewExtension = Extension( Function( isStatic: false, - name: "dequeueReusableSupplementaryViewOfKind", + name: "dequeueReusableSupplementaryViewOfKind", + generics: "T: \(Type._UICollectionReusableView)", parameters: [ Function.Parameter(name: "elementKind", type: Type._String), Function.Parameter(name: "withReuseIdentifier", localName: "identifier", type: ReuseIdentifier.type), @@ -150,7 +158,8 @@ let ReuseIdentifierUICollectionViewExtension = Extension( Function( isStatic: false, - name: "registerNib", + name: "registerNib", + generics: "T: \(NibResourceProtocol.type) where T: \(ReusableProtocol.type), T.T: UICollectionViewCell", parameters: [ Function.Parameter(name: "nibResource", type: Type(name: "T")) ], @@ -160,7 +169,8 @@ let ReuseIdentifierUICollectionViewExtension = Extension( Function( isStatic: false, - name: "registerNibs", + name: "registerNibs", + generics: "T: \(NibResourceProtocol.type) where T: \(ReusableProtocol.type), T.T: UICollectionViewCell", parameters: [ Function.Parameter(name: "nibResources", type: Type(name: "[T]")) ], @@ -170,7 +180,8 @@ let ReuseIdentifierUICollectionViewExtension = Extension( Function( isStatic: false, - name: "registerNib", + name: "registerNib", + generics: "T: \(NibResourceProtocol.type) where T: \(ReusableProtocol.type), T.T: UICollectionReusableView", parameters: [ Function.Parameter(name: "nibResource", type: Type(name: "T")), Function.Parameter(name: "forSupplementaryViewOfKind", localName: "kind", type: Type._String) @@ -181,7 +192,8 @@ let ReuseIdentifierUICollectionViewExtension = Extension( Function( isStatic: false, - name: "registerNibs", + name: "registerNibs", + generics: "T: \(NibResourceProtocol.type) where T: \(ReusableProtocol.type), T.T: UICollectionReusableView", parameters: [ Function.Parameter(name: "nibResources", type: Type(name: "[T]")) ], diff --git a/README.md b/README.md index fc8b870f..0e1ff809 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,12 @@ _Tool to get strong typed, autocompleted resources like images, cells and segues ## Why use this? -You currently type: +It makes your code that uses resources: +- **Fully typed**, less casting and guessing what a method will return +- **Compiletime checked**, no more incorrect strings that make your app crash on runtime +- **Autocompleted**, never have to guess that image name again + +Currently you type: ```swift let icon = UIImage(names: "settings-icon") let cell = dequeueReusableCellWithReuseIdentifier("textCell", forIndexPath: indexPath) as? TextCell @@ -12,16 +17,11 @@ performSegueWithIdentifier("openSettings") With R.swift it becomes: ```swift -let icon = R.images.settingsIcon +let icon = R.image.settingsIcon let cell = dequeueReusableCellWithReuseIdentifier(R.reuseIdentifier.textCell, forIndexPath: indexPath) performSegueWithIdentifier(R.segue.openSettings) ``` -It makes your code that uses resources: -- **Fully typed**, less casting and guessing what a method will return -- **Compiletime checked**, no more incorrect strings that make your app crash on runtime -- **Autocompleted**, never have to guess that image name again - ## Usage After installing R.swift into your project you can use the `R`-struct to access resources. If the struct is outdated just build and R.swift will correct any missing/changed/added resources.