diff --git a/R.swift/func.swift b/R.swift/func.swift index 612ac455..bbf2c968 100644 --- a/R.swift/func.swift +++ b/R.swift/func.swift @@ -145,6 +145,16 @@ func nibStructForNib(nib: Nib) -> Struct { return Struct(name: nib.name, vars: instanceVars, functions: [instantiateFunc] + viewFuncs, structs: []) } +// Reuse identifiers + +func reuseIdentifierStructFromReuseIdentifierContainers(containers: [ReuseIdentifierContainer]) -> Struct { + let reuseIdentifierVars = containers + .flatMap { $0.reuseIdentifiers } + .map { Var(name: $0, type: Type._String, getter: "return \"\($0)\"") } + + return Struct(name: "reuseIdentifier", vars: reuseIdentifierVars, functions: [], structs: []) +} + // Validation func validateAllFunctionWithStoryboards(storyboards: [Storyboard]) -> Function { diff --git a/R.swift/main.swift b/R.swift/main.swift index 92cd20ab..4527b2f4 100644 --- a/R.swift/main.swift +++ b/R.swift/main.swift @@ -33,12 +33,15 @@ inputDirectories(NSProcessInfo.processInfo()) let nibs = findAllNibURLsInDirectory(url: directory) .map { Nib(url: $0) } + let reuseIdentifierContainers = nibs.map { $0 as ReuseIdentifierContainer } + storyboards.map { $0 as ReuseIdentifierContainer } + // Generate let structs = [ imageStructFromAssetFolders(assetFolders), segueStructFromStoryboards(storyboards), storyboardStructFromStoryboards(storyboards), nibStructFromNibs(nibs), + reuseIdentifierStructFromReuseIdentifierContainers(reuseIdentifierContainers) ] let functions = [ diff --git a/R.swift/types.swift b/R.swift/types.swift index 43240108..209573a9 100644 --- a/R.swift/types.swift +++ b/R.swift/types.swift @@ -144,6 +144,10 @@ struct Struct: Printable { /// MARK: Asset types +protocol ReuseIdentifierContainer { + var reuseIdentifiers: [String] { get } +} + struct AssetFolder { let name: String let imageAssets: [String] @@ -156,11 +160,12 @@ struct AssetFolder { } } -struct Storyboard { +struct Storyboard: ReuseIdentifierContainer { let name: String let segues: [String] let viewControllers: [ViewController] let usedImageIdentifiers: [String] + let reuseIdentifiers: [String] init(url: NSURL) { name = url.filename! @@ -174,6 +179,7 @@ struct Storyboard { segues = parserDelegate.segues viewControllers = parserDelegate.viewControllers usedImageIdentifiers = parserDelegate.usedImageIdentifiers + reuseIdentifiers = parserDelegate.reuseIdentifiers } struct ViewController { @@ -182,9 +188,10 @@ struct Storyboard { } } -struct Nib { +struct Nib: ReuseIdentifierContainer { let name: String let rootViews: [Type] + let reuseIdentifiers: [String] init(url: NSURL) { name = url.filename! @@ -196,6 +203,7 @@ struct Nib { parser.parse() rootViews = parserDelegate.rootViews + reuseIdentifiers = parserDelegate.reuseIdentifiers } } @@ -205,6 +213,7 @@ class StoryboardParserDelegate: NSObject, NSXMLParserDelegate { var segues: [String] = [] var viewControllers: [Storyboard.ViewController] = [] var usedImageIdentifiers: [String] = [] + var reuseIdentifiers: [String] = [] func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: [NSObject : AnyObject]!) { switch elementName { @@ -223,6 +232,10 @@ class StoryboardParserDelegate: NSObject, NSXMLParserDelegate { viewControllers.append(viewController) } } + + if let reuseIdentifier = attributeDict["reuseIdentifier"] as? String { + reuseIdentifiers.append(reuseIdentifier) + } } func viewControllerFromAttributes(attributeDict: [NSObject : AnyObject], elementName: String) -> Storyboard.ViewController? { @@ -244,6 +257,7 @@ class StoryboardParserDelegate: NSObject, NSXMLParserDelegate { class NibParserDelegate: NSObject, NSXMLParserDelegate { let ignoredRootViewElements = ["placeholder"] var rootViews: [Type] = [] + var reuseIdentifiers: [String] = [] // State var isObjectsTagOpened = false; @@ -265,6 +279,10 @@ class NibParserDelegate: NSObject, NSXMLParserDelegate { } } } + + if let reuseIdentifier = attributeDict["reuseIdentifier"] as? String { + reuseIdentifiers.append(reuseIdentifier) + } } func parser(parser: NSXMLParser!, didEndElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!) {