-
NSOperation的认知
【官方文档】 NSOperation目录1.NSOperation简介2.NSOperation和NSOperationQueue的基本使用2.1 创建任务2.2 创建队列2.3 将任务添加到队列中3.操作依赖4.一些其他方法 1.NSOperation简介NSOperation是Apple提供给开发者的一套多线程解决方案,实际上是基于GCD的一套更高级封装,完全Objective-C代码。简单、易用、代码可读性高。 NSOperation需要配合NSOperationQueue来实现多线程,因为默认情况下 NSOperation单独使用时是系统同步执行操作,并没有开启新线程的能力,只有配合NSOperationQueue才能实现异步执行 因为NSOperation是基于GCD的,那么使用起来也和GCD差不多,其中,NSOperation相当于GCD中的任务,而NSOperationQueue则相当于GCD中的队列。NSOperation实现多线程的使用步骤分为三步: 1.创建任务:先将需要执行的操作封装到一个NSOperation对象中 2.创建队列:创建NSOperationQueue对象 3.将任务加入到队列中,然后将NSOperation对象加入到NSOperationQueue中,之后,系统就会从Queue中读取出来,在新线程中执行操作。 以下我们来看下NSOperation和NSOperationQueue的基本使用 2.NSOperation和NSOperationQueue的基本使用NSOperation是一个抽象类,不能封装任务,我们只有使用它的子类来封装任务。有三种方式来封装任务,如下: 1.使用子类NSInvocationOperation 2.使用子类NSBlockOperation 3.自定义一个类派生自NSOperation,定义一些相应的方法 2.1 创建任务比如:我们先不使用NSOperationQueue,而是单独使用NSInvocationOperation和NSBlockOperation,分别如下: 2.1.1 NSInvocationOperation 12345678- (void)invocationOp { NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run) object:nil]; [op start];}- (void)run { NSLog(@"%@", [NSThread currentThread]);} 输出结果如下,证明了单独使用NSInvocatio more -
GCD的一般认知
GCD (Grand Central Dispatch)GCD两个核心概念:任务和`队列 任务任务就是执行操作的意思,也就是block那段代码。执行操作有两种:同步执行和异步执行。同步执行(sync):阻塞主线程并执行任务,不会开启新线程任务异步执行(async):不会阻塞主线程,会开启新线程执行任务,在后台执行 队列这里的队列就是任务队列,即用来存放任务的队列。队列是一种特殊的线性表,采用先进先出(FIFO)的原则,每次新任务都会被插入到队列尾部,而执行队列中的任务时,会从队列头部开始读取并执行。GCD中有两种队列:串行队列和并行队列1.并行队列(DISPATCH_QUEUE_CONCURRENT):可以多个任务同时进行,也就会开启多个线程执行任务。交替执行。2.串行队列(DISPATCH_QUEUE_SERIAL):任务一个接着一个执行,也就是一个任务执行完后,下一个任务就开始。一个接着一个执行。 队列的创建12345// 串行队列dispatch_queue_t queue= dispatch_queue_create("my_queue_serial", DISPATCH_QUEUE_SERIAL);// 并行队列dispatch_queue_t queue= dispatch_queue_create("my_queue_concurrent", DISPATCH_QUEUE_CONCURRENT); GCD默认提供了全局队列和主队列1.全局队列 dispatch_get_global_queue ,全局队列就是并行队列,供整个应用使用; 需要两个参数,第一个是队列优先级(DISPATCH_QUEUE_PRIORITY_DEFAULT),第二个0即可(官方文档说:For future use)2.主队列 dispatch_get_main_queue ,主队列就是串行队列,在应用启动时,就创建好了,所以我们要用的时候就直接拿来用而不需要创建 任务和队列的组合1.并行队列 + 同步执行2.并行队列 + 异步执行3.串行队列 + 同步执行4.串行队列 + 异步执行 还有两个特殊组合1.主队列 + 同步执行(会死锁并崩溃)2.主队列 + 异步执行 并行队列 串行队列 主队列 同步(顺序执行) 阻塞主线程,没有开启新线程,串行执行任务 阻塞主线程,没有开启新线程,串行执行任务 阻塞主线程,没有开启新线程,串行执行任务(会死锁导致崩溃) 异步(并发执行) 不阻塞主线程,有开启新线程,并行执行任务 不阻塞主线程,有开启新线程,并行执行任务 不阻塞主线程,没有开启新线程,串行执行任务 看一看几种死锁原因12345678910111213141516171819202122232425262728 more -
iOS 常识
一、常用架构MVC (Model View Controller) 模型,视图,控制器,模型负责提供数据,视图负责显示, 控制器的作用就是确保模型和视图的同步,一旦M改变,V就应该立即更新。MVC其实就是一个环形的形式 https://baike.baidu.com/item/MVC框架/9241230?fr=aladdin&fromid=85990&fromtitle=MVC MVP (Model View Presenter) 模型,视图,是从MVC演变而来,它们相通点就是Controller和Presenter 都是负责处理业务逻辑,但是它们也有很大的区别,就是把Model和View进行了分离,在MVP中,视图并不是直接使用模型,而是通过Presenter来进行的, 也就是说业务在Presenter内部,数据获取和更新在Model内部,如果视图需要更新,就需要通过Presenter; Presenter与View的交互可以是间接的,可以通过接口来更新view,如果view较为复杂,也可以做一个adapter。 https://baike.baidu.com/item/MVP模式/10961746 MVVM (Model View View Model) 一般用在用户控件上,该模式是使用的数据绑定基础架构, MVVM是由MVP演变过来的,所以一些事件和命令相关的东西就放在了MVVM中的VM,其实也就是相当于MVP中的P https://baike.baidu.com/item/MVVM/96310?fr=aladdin ORM (Object Relational Mapping) 对象关系映射就是一种为了解决面向对象语言与关系型数据库而存在的简易数据的映射,因为是基于对象模型的。 如iOS CoreData,.NET Entity Framework 二、iOS系统的内存分配1.栈区(stack)由编译器自动分配并释放,先进后出2.堆区(heap)由程序员分配和释放,如果程序员不释放,在程序也就是该进程结束时,会由操作系统回收3.全局区(也叫作静态区,static)存放全局变量和静态变量的,未初始化的存在bss段,已初始化的存放在data段4.常量区 存放常量的,程序结束后由系统释放内存空间5.代码区 存放程序函数的二进制代码 异步绘制我们进行UITableViewCell重用时,可以把cell的高度进行缓存,以便于下次使用时,直接读取而不用重新计算,计算消耗性能。异步队列进行绘制UI,使用CoreGraphics框架,其中CoreText是一个文本处理引擎,我们就用这个,它的坐标系统左下角为0,0点 离屏渲染: 1.当设置CALayer的圆角,alpha,阴影,光栅化,抗锯齿,渐变 more -
Autolayout代码编写基本使用
第一种 代码如下:1234UIView *redView = [[UIView alloc] init];redView.translatesAutoresizingMaskIntoConstraints = NO;redView.backgroundColor = UIColor.redColor;[self.view addSubview:redView]; 1234567891011//设置高度[redView addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:40.0]];//设置左边距[self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:20.0]];//设定顶边距[self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:20.0]];//设置右边距[self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:-20.0]]; 换做使用VFL的话,代码如下:1234567NSDictionary *views = NSDictionaryOfVariableBindings(redView);CGFloat hei more -
看完美剧《Westworld》
前几个晚上我在腾讯视频看完《西部世界》,原名《Westworld》,去年上映的。该剧讲述了由一座巨型高科技以西部世界为主题的成人乐园,提供给游客杀戮与性欲的满足,随着接待员有了自主意识和思维,他们开始怀疑这个世界的本质,进而觉醒并反抗人类的故事。关键是该剧的接待员都是AI机器人,它们的智力、情感、意识等等方面已经基本达到和人类同等,不知道我们的未来的AI机器人是否也能达到这个的境界?😄我希望AI发展越来越棒,以后人们(其实是我)就越来越懒啦😂 第1集 科技乐园造人吸金叛逆意识暗中萌生第2集-老鸨忆起残忍往事黑衣人肆杀接待员第3集 - 自由思维无序出现第4集 - 伯纳德放任自由意识第5集 - 乐园隐藏创始人残留意识第6集 - 已故创建者 故意留程序第7集 - 新任总裁要搞事 福特地位受威胁第8集 - 福特掩盖真相 梅芙擅取权限第9集 - 阿诺德身份浮出水面第10集- 深层记忆悄然觉醒乐园变革拉开帷幕 more -
iOS事件响应链
iOS事件是如何响应的?iOS获取到了用户的“点击”这一行为后,把这个事件封装成UITouch和UIEvent形式的实例,然后找到当前运行的程序,并逐级寻找能够响应这个事件的对象,直到没有响应者响应。这个过程就叫做事件的响应链。 UITouch是触摸对象 UIEvent是事件对象 12345//根据坐标返回响应点击的对象- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event; // recursively calls -pointInside:withEvent:. point is in the receiver's coordinate system//根据坐标返回事件是否发生在本视图内- (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event; // default returns YES if point is in bounds 引用 http://www.cocoachina.com/ios/20160113/14896.html more -
You should know the easy-to-use objc_msgSend
Too many arguments to function call, expected 0, have 2When I created an new iOS project, I got this error as shown below. But why I got this error? It’s easy to know because of Enabling Strict Checking of objc_msgSend Calls in default mode. To solve itSelect your project -> select your target -> Build Settings -> Search by keyword “objc_msgSend”, only one item be shown -> toggle it to NO To make use of objc_msgSend for message forwarding12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970#import "ViewController.h"#import <objc/message.h>@interface ViewController ()@end@implementation ViewController//1.无参无返回值- (void)run1 { NSLog(@"1.无参无返回值");}//2.有参无返回值- (void)run2:(NSArray *)parameters { NSLog(@"2.有参无返回值 name=%@ age=%d", parameters[0], [parameters[1] intValue]);}//3.无参有返回值- (NSString *)run3 { return @"无参有返回值";}//4.有参有返回值- (int)run4:(NSString *)name age:(int)age height:(float)height { NSLog(@"4.有参有返回值 name=%@, age=%d, height=%lf", name, age, height); return age + height;}- (vo more -
如何提高雅思/托福听力?
2017.11.04==========================================================================最近比较忙,陆陆续续的做听力,效果还是比较好的,最近我在YouTube上给一些英国的或者美国的短视频添加中文或者英文字幕,我觉得,其实这就是一种现实的锻炼吧;一般专门挑选10分钟以内的视频做翻译,因为,如果你那几十分钟的视频做翻译,真的很花时间,所以,每天给那种只有3-5分钟的视频做个翻译,添加个字幕,也不会太占用你的时间。 现在5分钟的视频,我一般需要一个小时翻译完,希望后面的再接再厉,缩短时间 2017.10.14==========================================================================经过几天的听写,效果还是不错的,很多时候连一些简短的介词和连读也熟悉了,narrator和speakers的说话发音特别清晰。。。接下来,我是依次尝试给托业的听力也做做听写 2017.10.05 第三天练习==========================================================================我开发完了,叫做雅思精听,现在在上传到App Store过程中,需要一周时间;另外这是主页:https://ielts-listening-20171005.firebaseapp.com/ 需要连接VPN才能访问 2017.10.03 第一天练习==========================================================================今天上午练习听写,从IELTS 12的Section 1开始的,但是每次想回退几秒,重听一下时,传统的雅思app的听力播放器很坑,不好点,或者不好用。于是下定决定,自己开发一个吧 2017.10.02 找训练方法========================================================================== 1.我遇到的问题我最近发现我的英语听力没有质的突破,也就是基本没怎么提高,还是和一个月以前差不多。虽然我每天都在听英语新闻,听雅思或者托福的听力,还有其他英语素材,但是没太大效果啊??? 在百度上找了找资料,大部分人说需要1.精听,可是精听的具体细节究竟是什么了?要用哪些2.听力资料了?每天大约花费3.多长时间? 2.选择精听听力资料雅思/托福听力考试是非常注重细节记录的,不管是选择题还是填空题都需要对文章细节内容有一个良好的把握。如果光有单词基础,却无法把它们有机结合起来,自然是做不好题目的。而做精听练习恰好能弥补考生对听力原文的细节把握 more -
Safari Extension Development
Making your own Safari ExtensionCase 1https://developer.apple.com/library/content/documentation/Tools/Conceptual/SafariExtensionGuide/UsingExtensionBuilder/UsingExtensionBuilder.html Case 2Introductionhttps://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/SafariAppExtension_PG/ Creating and Running Your First Safari App Extensionhttps://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/SafariAppExtension_PG/CreatingandTestingYourFirstSafariAppExtension.html Injecting a Script into a Pagehttps://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/SafariAppExtension_PG/AddingScriptContent.html#//apple_ref/doc/uid/TP40017319-CH7-SW1 Adding a Toolbar Itemhttps://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/SafariAppExtension_PG/AddingaToolbar.html#//apple_ref/doc/uid/TP40017319-CH9-SW1 Access and Permissionshttps://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/SafariAppExtension_PG/DeterminingWhichPagesanExtensionCanAccess.html#//apple_ref/doc/uid/TP40017319-CH10-SW1 more -
iOS的属性关键字修饰符解释weak、assign、atomic和nonatomic等
weak 只能修饰派生自NSObject类或协议,因为NSObject即是类,也是协议 在对象的引用计数器为0时,对象的指针会自动指向nil 可以解决循环引用问题 assign 可以修饰基础数据类型,也可以修饰派生自NSObject类的对象 在对象的引用计数器为0时,对象的指针不会自动指向nil 可以解决循环引用问题 atomic 是Objective-C默认修饰的 原子性的 之所以线程安全,是因为只能编译器在生成setter和getter时,方法内部会有一个@synchronized()的同步互斥锁 线程不安全,是因为在多线程中操作对象时,比如:NSArray的addObject:方法,它是没有锁的,所以为了保证安全,我们必须自己加锁 速度慢 nonatomic 不是Objective-C默认修饰的,需要手动添加 非原子性 线程不安全 速度快 strong Objective-C对象默认创建时,就是强引用 修饰Objective-C对象的,创建时为引用计数为1 不管是修饰可变属性还是不可变属性,只要使用了赋值号,就是增加了一个指针指向同一块内存地址 copy 修饰不可变属性时,是浅拷贝,意思是:只要使用了赋值号,就是增加了一个指针指向同一块内存地址 修饰可变属性时,是深拷贝,意思是:只要使用了赋值号,就是新开辟了一块内存空间 __block 默认情况下,局部变量只可以被block访问 但是用它来修饰局部变量,就可以在block代码块里修改局部变量的值,因为这个局部变量被拷贝了一份到block代码块里,如果是指针就是拷贝了一份指针 __weak 就是将OC的对象改变成弱引用指针 注意一个block代码块是否会造成循环引用要看这个block所属的类是对这个block什么修饰,如果是strong和copy,则这个block代码块一定是强引用,且在外部使用这个block代码块时,使用self,就一定要转换成__weak形式;或者在这个block所属的类里对这个block修饰为assign或者weak,则在外部使用这个block代码块时,self不用转换为__weak形式。 more -
iOS应用审核 - Phased Release for Automatic Updates (阶段性自动更新发布)
Phased Release for Automatic Updates (阶段性自动更新发布)官方解释官方地址:https://itunespartner.apple.com/en/apps/faq/Managing%20Your%20Apps_Submission%20Process 以下将Phased Release for Automatic Updates 翻译成“ 阶段性自动更新发布 ”,本文所有篇幅是对官方文档的部分(该部分是Phased Release for Automatic Updates)翻译解释。1. 什么是阶段性自动更新发布?在iTunes Connect,你可以开启Phased Release for Automatic Updates,那就意味着,你发布了一个阶段性更新的iOS应用。在阶段性更新发布版本中,7天之内,你的应用会以百分比的形式来增量更新。在阶段性发布的版本期间内,你的应用会每天都显示在iTunes Connect上,并且部分用户会完成更新。当然,所有的已安装过你的应用的用户也可以选择App Store手动更新,新用户会一直都能看到你最近发布的“可供销售”的版本。 如果你发现在阶段性更新过程中,你的应用有某些缺陷,你可以在任何时间内暂停阶段性更新,这个时间持续30天,不管暂停有多少次。 2. 如何阶段性发布我的应用?阶段性发布一个更新版本: 在iTunes Connect首页,点击我的应用,然后选择一个应用; 在左边的列表,点击你想要提交的版本的应用; 在Phaed Release for Automatic Updates区域,选择 Release update over a 7-day period. 点击右上角保存。 3. 在阶段性发布中,成百分比例的用户是如何每天完成自动更新?自动更新打开时是任意选择的,这基于用户的Apple ID,而不是用户的设备。如果一个用户有多个设备,每个设备都开启了自动更新,那么当一个应用在阶段性发布更新时, 他们会在同一时间段内收到自动更新的提示。 4. 在阶段性发布中,我能每天为用户设置自动更新的百分比值吗?不能,因为在阶段性发布中,百分比的用户每天完成自动更新的图表如下显示,当然也会显示在iTunes Connect上 天数 百分比 第一天 1% 第二天 2% 第三天 5% 第四天 10% 第五天 20% 第六天 50% 第七天 100% 5. 我能针对阶段性发布的应用进行特定的统计数据吗?不能,因为不可能针对指定统计数据的用户进行,如年龄,性别,地区,设备信息,系统系统,设备类型的查看。用户的更新是随机选择的。 6. 在应用阶段性发布中,用户完成了自动更新会被通知到吗?不会被通知到 7. 我可以取消我的应用版本的阶段性更新 more -
iOS Reverse - (2) Theos introduction, installation and usage
1.IntroductionTheos is a jailbreak development tool written and shared on GitHub by a friend, Dustin Howett (@DHowett). Compared with other jailbreak development tools, Theos’ greatest feature is simplicity: It’s simple to download, install, compile and publish; the built-in Logos syntax is simple to understand. It greatly reduces our work besides coding.Additionally, iOSOpenDev, which runs as a plugin of Xcode is another frequently used tool in jailbreak development, developers who are familiar with Xcode may feel more interested in this tool, which is more integrated than Theos. But, reverse engineering deals with low-level knowledge a lot, most of the work can’t be done automatically by tools, it’d be better for you to get used to a less integrated environment. Therefore I strongly recommend Theos, when you use it to finish one practice after another, you will definitely gain a deeper understanding of iOS reverse engineering. 2.Install and configure Theos2.1 Install Xcode and Command Line ToolsMost iOS developers have already installed Xcode, which contains Command Line Tools. For those who don’t have Xcode yet, please download it from Mac AppStore for free. If two or more Xcod more -
iOS Reverse - (1) The basic tools of use of OpenSSH, iFile, MTerminal and syslogd
1.OpenSSHOpenSSH will install SSH service on iOS (as shown in figure below). Only 2 commands are the most commonly used: ssh is used for remote logging, scp is used for remote file transfer. The usage of ssh is as follows: 1ssh user@iOSIP For instance:1snakeninnysiMac:~ snakeninny$ ssh mobile@192.168.1.6 The usage of scp is as follows:1.Copy a local file to iOS1scp /path/to/localFile user@iOSIP:/path/to/remoteFile For instance:1snakeninnysiMac:~ snakeninny$ scp ~/1.png root@192.168.1.6:/var/tmp/ 2.Copy a file from iOS to the local system1scp user@iOSIP:/path/to/remoteFile /path/to/localFile For instance:1snakeninnysiMac:~ snakeninny$ scp root@192.168.1.6:/var/log/syslog ~/iOSlog These two commands are relatively simple and intuitive. After installing OpenSSH, make sure to change the default login password “alpine”. There’re 2 users on iOS, i.e. root and mobile, we need to change both passwords like this:1234FunMaker-5:~ root# passwd root Changing password for root.New password:Retype new password: FunMaker-5:~ root# passwd mobile Changing password for mobile. New password:Retype new password: If we forget to change the default password, there’re chances that viruses like Ikee logi more -
iOS App Dumps Encrypted Shell and Disassembling
OverviewSoftware reverse engineering refers to the process of deducing the implementation and design details of a program or a system by analyzing the functions, structures or behaviors of it. When we are very interested in a certain software feature while not having the access to the source code, we can try to analyze it by reverse engineering. Although the recipe of Coca-Cola is highly confidential, some other companies can still copy its taste. Although we don’t have access to the source code of others’ Apps, we can dig into their details of reverse engineering. PDF Document FOR MORE DETAILS OF iOS REVERSE ENGINEERING, SEE HERE. iOS应用逆向工程(第二版) Chinese BlogiOS逆向 - 获取AppStore上的应用的所有头文件和源文件 http://blog.csdn.net/u013538542/article/details/70196590 What’s the main purpose of this article saying? 1.Browses all directories on the iPhone;2.SSH login to the jailbreak iPhone;3.Dumps the encrypted App which is downloaded from the App Store and shows you a decrypted Mach-O file(It indicates the binary file of the IPA);4.Gets all header files and disassembles the binary file of the App, shows you the pseudo code of Objective-C;5.Copies files from Mac to iPhone, vice versa. Let’s get prepa more -
iOS 通过URL地址来安装应用
iOS应用通过URL地址来安装完整下载地址1itms-services://?action=download-manifest&url=https://www.domain.cn/d/up/package/my.application.ipa.plist 其中plist文件具体内容是1234567891011121314151617181920212223242526272829303132333435363738394041424344454647<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict> <key>items</key> <array> <dict> <key>assets</key> <array> <dict> <key>kind</key> <string>software-package</string> <key>url</key> <string>https://www.yourdomain.cn/download/2.1.0-10637/test.116d7.56d82df.20170122.test.domain.cn.ipa.plist</string> </dict> <dict> more