Teach you how to implement a component architecture from 0 to 1


The main idea of this component architecture is to teach you how to assemble a project from scratch. Of course, there are many holes in components. Thanks a lot for your help from Mr. Wang Shunzi.

If you love my article, I can focus on micro-blog: Yuan Zheng Seemygo, can also be small brother iOS training course, we understand. The follow-up will be more updates, have any questions, welcome Jane Zheng Yuan Book message Seemygo…

Why do you want to assemble?

  • As the project continues to iterate, each module becomes more complex, the modules depend on each other, and each module may have a common business logic that makes the whole project more cumbersome to maintain.
  • Component can be used to separate each business logic and module management, so that it is more convenient to maintain, and each developer only needs to care about their own modules.

Component benefits

  • Division of labor is more clear, improve development efficiency
  • Reusability is better, and can quickly form more App

Component-based thinking

  • Just like wrapper controls, complex controls are generally encapsulated, and components are simply drawn out of each module as a small project and then integrated into a single project.

How to assemble

  • Use the cocoapods management component of the development of podspec: to describe their component engineering code directory and resource directory in which, also has its own component engineering relies on other frameworks, we will according to the podspec guidelines to introduce its own warehouse code. The command: pod spec create spec file name / / create pod index database, fixed wording, and the definition of the index database the name is s, following by s, will be able to get the index: Spec.new do library Pod: |s| / / s.name set name = “HttpManager” / / set the version number s.version = “0.0.1” / / set s.summary = “A short the description of HttpManager.” / / set details s.description = “Good” / / set up warehouse homepage s.homepage = “http://EXAMPLE/HttpManager” / / set license s. License = “MIT” / / author set s.author = {“iThinkerYZ” =&gt “690423479@qq.com”;} / / set the warehouse source said, where can I find component engineering: git s.source = {=> =&gt: tag “,” #{s.version} “;} / / set the source file path => is not the entire project file, but own package code, after the other engineering introduction, it will introduce the code here. S.source_files = “HttpManager/Classes/**/*.{h m}” / / s.dependency = ” which rely on third party component engineering framework / / s.frameworks =’UIKit’,’MapKit’ component engineering which rely on native framework s.resource_bundles = {} / / component engineering picture resources podspec file end s.source_files note: just describe the component code is good, do not describe the entire project file, all file integration go wrong way: s.source_files = “HttpManager” * *: all documents said: because the * wildcard, not essential. S.source_files = “HttpManager/Classes/**/*.{h m}”, said the component code all HttpManager/Classes files in the podspec directory of the default will automatically track the path of the podspec file to the directory, because the current in podspec files. What is the File which files are automatically tracked. HttpManager/Classes/a.h matches should be “HttpManager/Classes/**”, which means that after the HttpManager/Classes/a.h there is nothing, it will not find, and directly match to. Podspec file note: s.description: not attention to the empty podspec file: s.license: cannot fill in, must have such agreements, such as (MIT) podFile file: Specifies the main engineering load which component library, which describe the corresponding podspec component library file in which you know where to go, loading the component code. Create command: pod init cocoapods can load the remote warehouse can also load the local warehouse, general loading remote warehouse. How to load the local warehouse code? Local warehouse code have a podspec file describing where loading component code who need to introduce local warehouse code to create Podfile Podfile podspec: Specifies the file in which pod’HttpManager’, path =&gt’../HttpManager’;../HttpManager: Podfile on the back of a directory, enter the HttpManager can find podspec where Podfile will automatically follow the directory where
Teach you how to implement a component architecture from 0 to 1
Podfile file.Png
Teach you how to implement a component architecture from 0 to 1
podspec file.Png
  • Create a way: create component engineering project, the assembly code on the Class, as long as the spec description, component code will automatically load the Class generated spec description file, loading the component engineering component code which is specified in the. Two: use the cocoapods command: pod lib create component code name of this command will automatically generate a set of component code engineering test code, and the Git
    Teach you how to implement a component architecture from 0 to 1
    Snip20170223_16.png management will generate podspec, describe the component code where good. How to use the component engineering automatic code generation? You need to put your assembly code corresponding to the file, in the Class is not enough, that no method is introduced into the component code.H file. To pod install, pod install Example for not re engineering, did not know Pod update, pod install: re let pod library and on the associated project files.

Cocopods principle

  1. According to the Podfile description, find the description file for the corresponding code library podspec
  2. Podspec describes where to go (s.source) to find the code library, and when you find it, you need to copy the files (s.source_files) to your project.

How do components (public remote repositories) be assembled?

  • Question: the code above all in the local warehouse, there is no remote management, which means only for their own use, others can not be used, if you want to open their own repository, so that all developers can get, how should I do?
  • Just push your warehouse code to the index library in cocoapods. Principle: we install cocoapods before, one step pod setup, this step is to download the cocoapods code all public warehouse index, save to the index of the pod through the repo command to view cocoapods
Teach you how to implement a component architecture from 0 to 1
pod path.png
  • How to put their own warehouse code uploaded to cocoapods to their own repository, create podspec, in fact is the essence of their own code warehouse podspec index file upload is good, you can directly use the pod lib create ordered his warehouse code for remote hosting, and open, so people can get to your warehouse code note: not remote warehouse you need to create the gitignore file, because the pod lib created to submit their own code to a remote warehouse warehouse git status: see, if do not want to file, you can use gitignore to ignore submitted to the local cache `git add submitted to the local git commit ` ` -m ” view remote warehouse warehouse address git remote (there is no remote address) git remote add origin address binding remote remote address push their code to a remote warehouse Git push origin master binds Tag to its own warehouse because cocoapods downloads the corresponding Tag’s remote code library based on the Tag of the code repository. Give yourself the warehouse version to add Tag tags, GIT tag -a 0.0.1 -m’0.0.1′, is the only local local label pushed to the remote server, GIT push –tags trunk trunk registration registration, not everyone can push, because cocoapods relies on the trunk server management, we need to push through trunk own podspec (cocoapods website) command: pod trunk register EMAIL [NAME] [NAME] NAME pod trunk register said: 58999050@qq.com Yuanzheng is not essential after successful verification, click on the mailbox is good, open a bit slow. Push your own podspec to cocoapods pod trunk push HttpManager.podspec –allow-warnings index CD note: you must enter into the podspec directory to run this code note: the s.version version of podspec documents in line with the latest Tag note: Po The DSPEC file in the s.source warehouse address cannot test whether the wrong index to the pod search index to find their own warehouse, in fact has been uploaded to cocoapods, only need to update the index file to update the pod index file? Rationale: pod setup succeeds in generating ~/Library/Caches/CocoaPods/search_index.json files, deleting the search_index.json file, and re executing the pod search, which updates the index

How is component (private remote warehouse)?

  • The core of the problem: some companies do not want to open source code, you can’t put in the cocoapods public library index, can not put in the local, because many people to support the creation of private development, cocoapods index database of their own, only need to put their code warehouse put in his private library is good.
  • How to create a private remote index database have a private remote index database hosting platform for local add private remote index library: pod repo add XMGSpec https://git.coding.net/iThinkerYZ520/XMGSpec.git, is behind the index database remote address his private library index added to its own private library: pod repo push XMGSpec XMGLib.podspec –allow-warnings, a local index repository will have their own private library. And the remote will, pod repo push will help us pushed to the remote index database.
  • How to use search pod’s own search index private warehouse own library description of pod install, to find, because the default is to find common index library need to add a source at the top of the Podfile file, where to go to find that. Source’https://git.coding.net/iThinkerYZ520/XMGSpec.git’but there is a problem, if you want to add the index, after the public such as AFN, can not find so also need to add a public library source source’https://github.com/CocoaPods/Specs.git’ # said the first to find a private,’~&gt in source’https://git.coding.net/iThinkerYZ520/XMGSpec.git’source’https://github.com/CocoaPods/Specs.git’ target for public private library ‘do’ test index pod’XMGLib’pod’AFNetworking’. 3.1.0′; end

Component-based upgrade

  • After each component should be constantly updated, perfect, how to manage?. Only the latest version of the code to bind tag, update the Podspec file to upload to the library version is good pod repo push XMGSpec XMGLib.podspec –allow-warnings project file when in use, the use of pod update will be able to load the latest version of pod update –no-repo-update assembly code.

Component resource

  • Where do components project resources go? Use pod lib create to create the component project, a Assets folder, and then put the picture that podspec specifies the resource file path s.resource_bundles warehouse code re pod install, will automatically generate the bundle file, save to this picture.
Teach you how to implement a component architecture from 0 to 1
Teach you how to implement a component architecture from 0 to 1
  • Because the podspec specifies the image resource that is the path, the path is automatically searched
  • How do I use the resources of component code? Before taking pictures are in the main bundle: NSBundle mainBundle, but not in the main components of the source code, bundle, is to get bundle in their bundle [NSBundle bundleForClass:self] framework, self said: the current class, the method of obtaining in the current class bundle because the current method in their framework, you can get yourself the bundle
    Teach you how to implement a component architecture from 0 to 1
NSBundle *selfBundle = [NSBundle bundleForClass:self] '; NSLog (@ "% @", selfBundle); / / bundle is not enough, bundle images in the XMGLib.bundle file to the full name of NSString *path / / / / picture = "XMGLib.bundle/ [selfBundle pathForResource:@ @2x.png UIImage picture" ofType:nil]; *image = [UIImage imageWithContentsOfFile:path];.
  • Note: possible items will be encountered, as shown in Figure bug
Teach you how to implement a component architecture from 0 to 1
  • Reason: the imported component code has Xib, but Xib cannot be found because Xib is a resource and cannot be imported as code, that is, you need to import it with resource_bundles.
    Teach you how to implement a component architecture from 0 to 1
  • Run: Xib cannot be found because Xib is stored in the bundle of the resource, so prefix is added
NSBundle *bundle = [NSBundle bundleForClass:[self class]]; / / get the current bundle name NSString *bundleName bundle.infoDictionary[@ = "CFBundleName"]; bundleName [NSString stringWithFormat:@ = "%@.bundle" bundleName]; / / Xib name splicing and Bundle name, or can not find the Xib NSString *nibName [NSString stringWithFormat:@ = "%@/XMGHomeRecommendCell", bundleName] [self.tableView registerNib:[UINib nibWithNibName:nibName bundle:[NSBundle bundleForClass:[self; class]]] forCellReuseIdentifier:ID];
  • Friendly reminder: if you use resources in the component, be sure to generate the component framework, you can not generate.A, otherwise the resources can not be obtained. Add the description use_frameworks in the podfile file!
Teach you how to implement a component architecture from 0 to 1
  • The difference between using use_frameworks and not using use_frameworks
Teach you how to implement a component architecture from 0 to 1
Teach you how to implement a component architecture from 0 to 1

Frame dependence

  • The podspec file describes: s.dependency’AFNetworking’automatically imports third party libraries when using components

How are components (divided into subcomponents)?

  • As the assembly continues to expand, business will be more and more, if not designated molecular components, we can sometimes do not need to import so much engineering business, will together into their own projects, resulting in their engineering unnecessary code too much, so in large companies usually for their project is to reduce weight, not the necessary code.
  • A good component is to have a good sub component, and when others load your component, you can load the corresponding component code according to your own needs
  • Take SDWebImage as an example, there are four sub components inside, and a component is used to load GIF pictures
Teach you how to implement a component architecture from 0 to 1
  • SDWebImage podspec file describes the sub components, fixed format Note: their sub components if rely on other sub components, must write on sub assemblies or sub assemblies can not be used, the following components depending on Core note: GIF and GIF cannot have the same name, ruby syntax, GIF is outside the search component name, gif is a variable for later access, if you use subspec, others into your code will be in accordance with the subspec framework, divided the folder structure, otherwise all code together, regardless of whether you before the division, such as SDWebImage will have a GIF folder and if you use subspec, you don’t need to describe the entire path folder, will cause the subspec partition no folder with the code, # commented before the description.
Teach you how to implement a component architecture from 0 to 1
S.subspec'GIF'do |gif| gif.ios.deployment_target ='7.0' gif.source_files ='SDWebImage/FLAnimatedImage/*.{h, m}'/ / set dependence, rely on their component sub component Core gif.dependency'SDWebImage/Core' gif.dependency'FLAnimatedImage','~> 1.0' gif.xcconfig = {'USER_HEADER_SEARCH_PATHS'=> $(inherited) $/FLAnimatedImage/FLAnimatedImage'} end (SRCROOT)
  • Distinguish between using subspec and not using subpec
Teach you how to implement a component architecture from 0 to 1
  • How to load the podfile file to describe the sub assembly / / will only introduce the GIF module pod’SDWebImage/GIF’