IOS开发入门之一步一步做一个iOS的静态库
白羽 2018-11-30 来源 :网络 阅读 473 评论 0

摘要:本文将带你了解IOS开发入门一步一步做一个iOS的静态库,希望本文对大家学IOS有所帮助。

    本文将带你了解IOS开发入门一步一步做一个iOS的静态库,希望本文对大家学IOS有所帮助。



        

0 前言

如果你一定要做静态库,就跳过这段吧。

本文没什么卵用啊,因为网上类似的参考资料很少哎。

是的,你没看错,因为可参考资料少,所以这篇并没有什么卵用。这不矛盾。为什么这么说咧?

参考资料少,是因为这个需求很少嘛。你看看那些大佬级别的库,都是开源的,而且都是让你直接用源码的啊。

因为Windows下的DLL思想在移动端应用级别的App开发中,已经没啥用了。特别是iOS,AppStore的APP禁止用动态库的好么。

当然,有时候我们没有选择,那就开始吧。

这里我们要解决俩个问题:(1)静态库对其他库的依赖的问题, (2)使用了category的问题

1 创建一个静态库工程

静态库可以是.a结尾的文件,也可以是.framework结尾的。网上很多资料,就不撸了。

(1)创建

如图所示,填上项目名字就可以了,我的是GrouchyLibrary,Grouchy是“爱抱怨的”的意思:

(2)然后么就开始写代码了额

(等等,这就结束了吗?)没有没有,要解决前面说的两个问题。

项目假设这样一个场景:我们从服务器请求数据,服务器返回的数据格式一般是JSON或者ProtoBuffer、XML的。我们希望对于请求的使用者来说,我们都得到一个Dictionary,JSON被很多组件内置支持。因此,为了兼容服务器返回的不同格式,我们需要一个能把XML文档处理成NSDictionary对象的工具。

首先,我们使用一个名为 GDataXMLNode的第三方组件。然后在此基础上做一个返回NSDictionary对象的扩展:GDataXMLDocument (GrouchyParse)。

嗯,一图胜千言,代码的差不多是这样的:

然后贴一下GDataXMLDocument (GrouchyParse)的代码


   

//

 

// GDataXMLDocument+GrouchyParse.h

 

// KedllNetLibrary

 

//

 

// Created by Sendor.Wang on 16/8/9.

 

// Copyright ? 2016年 Luke. All rights reserved.

 

//

 

 

 

#import "GDataXMLNode.h"

 

 

 

@interface GDataXMLDocument (GrouchyParse)

 

 

 

- (NSDictionary*)parse;

 

 

 

@end

 

 

 

 

 

 

//

 

// GDataXMLDocument+GrouchyParse.m

 

// KedllNetLibrary

 

//

 

// Created by Sendor.Wang on 16/8/9.

 

// Copyright ? 2016年 Luke. All rights reserved.

 

//

 

 

 

#import "GDataXMLDocument+GrouchyParse.h"

 

 

 

@implementation GDataXMLDocument (GrouchyParse)

 

 

 

- (NSDictionary*)parse

 

{

 

NSMutableDictionary *rootDict = [[NSMutableDictionaryalloc]init];

 

GDataXMLElement *root = [selfrootElement];

 

NSArray * attributes = [rootattributes];

 

if (attributes.count >0) {

 

NSMutableDictionary* attributesDict = [[NSMutableDictionaryalloc]initWithCapacity:attributes.count];

 

for (int i=0; i

 

GDataXMLNode *node = attributes[i];

 

attributesDict[node.name] = node.stringValue;

 

}

 

rootDict[@"attributes"] = attributesDict;

 

}

 

NSArray * childrenArray = root.children;

 

if (childrenArray.count >0) {

 

for (int i=0; i

 

GDataXMLNode *node = childrenArray[i];

 

[selfaddItem:[selfparseNode:node]withName:node.nametoDict:rootDict];

 

}

 

}

 

return rootDict;

 

}

 

 

 

 

 

- (id)parseNode:(GDataXMLNode *)node {

 

if([node XMLNode]->type == XML_TEXT_NODE) {

 

return [nodestringValue];

 

} else {

 

NSMutableDictionary *dict = [[NSMutableDictionaryalloc]init];

 

GDataXMLElement *element = (GDataXMLElement*)node;

 

NSArray * attributes = [elementattributes];

 

if (attributes.count >0) {

 

NSMutableDictionary* attributesDict = [[NSMutableDictionaryalloc]initWithCapacity:attributes.count];

 

for (int i=0; i

 

GDataXMLNode *node = attributes[i];

 

attributesDict[node.name] = node.stringValue;

 

}

 

dict[@"attributes"] = attributesDict;

 

}

 

NSArray * childrenArray = element.children;

 

if (childrenArray.count >0) {

 

for (int i=0; i

 

GDataXMLNode *node = childrenArray[i];

 

[selfaddItem:[selfparseNode:node]withName:node.nametoDict:dict];

 

}

 

}

 

return dict;

 

}

 

}

 

 

 

- (void)addItem:(NSDictionary *)item withName:(NSString*)name toDict:(NSMutableDictionary*)dict

 

{

 

NSString *arrayKey = [NSStringstringWithFormat:@"%@s", name];

 

if (dict[name]) {

 

NSMutableArray* array = [[NSMutableArrayalloc]initWithArray:@[dict[name], item]];

 

dict[arrayKey] = array;

 

[dict removeObjectForKey:name];

 

} elseif( dict[arrayKey] && [dict[arrayKey]isKindOfClass:[NSMutableArrayclass]]) {

 

[dict[arrayKey] addObject:item];

 

} else {

 

dict[name] = item;

 

}

 

}

 

 

 

@end

   


(3)然后编译,不通过

别慌,看看错误信息:

GDataXMLNode.h:41:9: 'libxml/tree.h' file not found。

libxml/tree.h文件没找到。这是什么鬼。原来GDataXMLNode依赖于iOS提供的库:libxml2.tbd.

[注意:我们如果在静态库中需要引用系统的动态库或者第三方静态库的时候,一般(绝大部分情况下)不直接把静态库或者动态库打入到我们的静态库包里面, 而是由静态库的使用者在APP中添加引用]

郑重其事的说了一通,其实就是说不要在静态库中引入libxml2.tbd。其实不管引不引入,都要解决'libxml/tree.h文件的问题。Google了一下(其实是bing,由于网络环境不好,用google的机会很少),这个文件位于XCode SDK目录下的usr/include/libxml2中,需要这样设置

左键点击左侧的项目,选择targets -> build settings -> search paths -> header search paths, 在右边双击,弹出编辑框:

点击“+”, 输入${SDK_DIR}/usr/include/libxml2,再次编译,ok.

(4)输出.h文件

和Java不同,Object C 需要输出头文件供使用者引入,就像我们刚才引入libxm/tree.h一样。

切换到Build Phases, 展开下方的Copy files, 点“+”, 在弹出的窗口中选择要导出的.h文件,以及这些文件引用的头文件(递归),一般就是所有.h文件。

2 静态库工程就算创建好了,下面我们来创建一个测试工程

(1)创建配置工程

就是建一个普通的APP工程,就不贴图和步骤了。如果你忘了把他们放在一个workspace里面,关掉其中一个,把另一个工程文件从find里面拖到XCode里面就可以了。

我建的是个Single View Application, 在ViewController.m 里面输入如下代码:


   

#import "ViewController.h"

 

#import "GDataXMLDocument+GrouchyParse.h"

 

 

 

@interface ViewController ()

 

@end

 

 

 

@implementation ViewController

 

- (void)viewDidLoad {

 

[superviewDidLoad];

 

// Do any additional setup after loading the view, typically from a nib.

 

NSString *xmlString =@"Hello World!";

 

GDataXMLDocument *xmlDoc = [[GDataXMLDocumentalloc]initW    

   

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之IOS频道!


本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程