转自:http://zappdesigntemplates.com/create-an-embedded-framework-in-xcode-with-swift/

We first heard this expression at WWDC last year in 2014, Embedded Framework. A frameworkor a library is usually a bunch of classes and objects put together into one big block to be able to reuse it in different applications. The same thing is happening here. An Embedded Framework is the same thing, the only restricion it cannot contain images or other resources.

Of course you can just simply avoid using these frameworks and modify your classes’ target membership settings, and you don’t have to worry about this whole framework thing.

 
 

BUT, since we are awesome and clever developers, we want to make our code also amazing and intelligent, we should organize them into frameworks. You can even have multiple ones if you desire. One for networking and another one for the data models.

For our example, we are going to create one Embedded Framework for all of our classes that we want to access later in a Today Extension for example, or in our Watch app.

 

1. Add a new Target for our Embedded Framework

As every time, we need to create a new target. To do this, go to File->New->Target…

 
 

There you need to select Framework&Library and Cocoa Touch Framework.

 
 

Give it a name, we’ll name it SabWeatherDataKit and select a language and you are good to go.

 
 

Xcode will create 2 new folders/groups for you, one that contains your framework files and the test target’s files and details.

 
 

2. Setting up our framework

Now that we created this framework, we need to use it in our app somehow. In this example, we are going to move all the classes that should be or could be used in other targets such as a Today Extension. For clarity, just simply select your classes, and drag them into the SabWeatherDataKit folder. That’s it. But we are not done yet. If you select one of those classes, and open the Utilities window (right sidepanel) it will show in the Target Membership window, that it still belongs to the app not to the framework, and that’s bad.

So with the file(s) still selected, uncheck the app, and check the checkmark for your framework.

 
 

Now what Xcode does, it adds those classes to your framework and remove it from your project.

And if you build now, Xcode will cry a lot, and you’ll break your monitor trying to figure out those errors and warnings. But calm down everything is fine.

What simply happened, is that Xcode doesn’t know where those files/classes are right now, so we need to make sure to import our Framework, and all of our classes and methods are set to public (obviously the ones that we want to reach . I mean the ones, that should be visible to other classes, objects.

Wherever, you want to use any of your classes that are in the Embedded Framework now, you need to import your framework, like so:

import SabWeatherDataKit

 

3. Setting up your framework and project to work together as a team

Next up you need to define which classes and functions you want to make visible to other classes, targets. For example you might want to allow a login method to be called in your app, but the url constructing method that gives the proper url for the login request not.

In Swift it is simple, you just put the ‘public’ key in front of your method, class or property (in your framework’s classes) and boom, they are visible to your app.

 
 

In our example, the WeatherParser doesn’t need to be a public class, nor needs to convert its methods to public, cause it will be only used inside the framework.

 

4. Name your framework classes right

I know this example doesn’t fit the title of this section, but think about this for a sec. Isn’t it easier for you or someone else to find out what your class is all about by just looking at it and read the filename? So instead of LocationHandler we could use WeatherKitLocationHandler and you immediately know that this class belongs to a framework. Just as other third party frameworks are doing this: Parse, AFNetworking, etc (those are not embedded frameworks, but why can’t you do it that way?).

 

5. Move all the classes that can be reused into your framework

Everything that can be reused in your app, your watchapp or extension, should be moved to a framework. It is not only easier for you to manage your code, but you can make pretty great things like using a Fascade pattern to create an object that can be used to generate different types for your views.

For example in our project, we could just have a method from the framework:

downloadWeatherDataForCurrentLocation(completionBlock: (succeeded: bool, weatherArray: [Weather]) -> ())

So you don’t need to initialise a locationManager object or care about all that, you just call one method and boom, you got the weather information for your current location.

 

Embedded Framework is an awesome tool to group your classes, objects and create a better iOS app. Combine it with a Today extension is pretty cool. But that is for the next tutorial, coming soon.

Until that, go ahead and download the free project and play around with it. If you have any questions or concerns, use the comment section below.

Create an Embedded Framework in Xcode with Swift的更多相关文章

  1. Chromium Embedded Framework 中文文档(简介)

    转自:http://www.cnblogs.com/think/archive/2011/10/06/CEF-Introduce.html 简介 Chromium Embedded Framework ...

  2. Chromium Embedded Framework

    关于CEF 近期由于工作需要开始研究了Google的Chromium Embedded Framework(CEF),这是一个基于Google Chromium开源代码的项目,使用CEF可以很方便的在 ...

  3. SDK 开发 .a .framework .bundle (xcode引用) 依赖sdk工程

    一. 静态库.a 1.创建静态库工程 Cocoa Touch Static Libray  ,然后可以创建一个测试视图 TestView 2.暴露头文件 -> Build Phases--> ...

  4. XCode下Swift – WebView IOS demo

    简介 我今天用Mac升级了XCode到8.1,Swift版本应该到了swift3,按网上的demo写webview的例子,报一堆错,整了一天才搞定,不想其他人踩坑了! XCode8.1 ,swift3 ...

  5. 在xcode中用 swift 进行网络服务请求

    xcode集成开发环境是运行于Mac苹果电脑上用于开发swift应用程序的工具,利用xcode可以很方便.直观的开发OS X和iOS系统所支持的应用程序. 1 开发环境: Mac OS 10.11 X ...

  6. How difficult is it to create a JavaScript framework?

    分享来自 quora 的一篇文章 https://www.quora.com/How-difficult-is-it-to-create-a-JavaScript-framework https:// ...

  7. Xcode 8 Swift 类似插件方法

    Xcode8 Swift使用技巧 1 option + cmd + / 可以弹出注释 2 color 然后加 enter 可以弹出颜色选择 3    #FIXME:  警告 4   #MARK: 备注 ...

  8. 分享你最喜欢的技巧和提示(Xcode,Objective-C,Swift,C...等等)

    http://www.cocoachina.com/ios/20151231/14846.html 笔者分享总结如下(本篇会不定期进行更新) : Objective-C 1.让Xcode的控制台支持L ...

  9. 『干货』分享你最喜欢的技巧和提示(Xcode,objective-c,swift,c...等等)

    亲爱的读者们,你们好 !年底将近,分享从过去一年你最喜欢的技巧和建议作为礼物送给新手们.提交你的最喜欢的迅速或objc琐事,实用的提示,意外的发现,实用的解决方法,没用的迷恋,或不论什么其它你认为今年 ...

随机推荐

  1. GC四大算法

    引言: 前面的文章提到,Heap包括了PSYoungGen.ParOldGen.Metaspace.JVM 在进行GC时,并非每次都对上面三个内存区域一起回收的,大部分时候回收的都是新生代.由于新生代 ...

  2. Box 黑科技 —— 支持手机端反编译 !Box 黑科技 —— 支持手机端反编译 !

    项目地址: Box 文末扫码获取最新安装包 . 前言 有将近一个月没有更新文章了,一方面在啃 AOSP ,消化起来确实比较慢.在阅读的过程中,有时候上来就会陷入源码细节,其实这是没有必要的.刚开始更多 ...

  3. 关于Struts.xml的路径配置以及对应的连接路径问题

    主要记住一点!: 1.   namespace的路径配置例如:/x:   2.  name名自由配置(主要用来是做action跳转方法用的) 3.  form表单中提交action时以这个格式提交(此 ...

  4. echo -e的扩展应用之颜色控制输出(字体+背景)

    echo -e 输出带颜色字体或者背景用法:example: echo -e "\033[41;36m something here \033[0m" 其中41的位置代表底色, 3 ...

  5. nyoj 23-取石子(一)(博弈)

    23-取石子(一) 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:20 submit:33 题目描述: 一天,TT在寝室闲着无聊,和同寝的人玩起了取 ...

  6. nyoj 78-圈水池 (凸包)

    78-圈水池 内存限制:64MB 时间限制:3000ms 特判: No 通过数:5 提交数:6 难度:4 题目描述: 有一个牧场,牧场上有很多个供水装置,现在牧场的主人想要用篱笆把这些供水装置圈起来, ...

  7. 用户环境变量 shell变量 别名

    常见用户环境变量: 环境变量 说明 LANG   HOME   LOGNAME 用户名 PATH   SHELL   PWD   查看环境变量用:env或者echo $LANG 设置用户环境变量:ex ...

  8. ef+Npoi导出百万行excel之踩坑记

            最近在做一个需求是导出较大的excel,本文是记录我在做需求过程中遇到的几个问题和解题方法,给大家分享一下,一来可以帮助同样遇到问题的朋友,二呢,各位大神也许有更好的方法可以指点小弟一 ...

  9. 【故障公告】数据库服务器 CPU 近 100% 引发的故障(源于 .NET Core 3.0 的一个 bug)

    非常抱歉,这次故障给您带来麻烦了,请您谅解. 今天早上 10:54 左右,我们所使用的数据库服务(阿里云 RDS 实例 SQL Server 2016 标准版)CPU 突然飙升至 90% 以上,应用日 ...

  10. Git的使用和基本概念理解

    参考:https://www.liaoxuefeng.com/wiki/896043488029600 一).git的使用: 1.创建版本库(Resopsitory),相当于一个目录,目录中所有的文件 ...