objective-c  objective-c中categories和extentions简介已关闭评论
12月 132019


Categories are an Objective-C language feature that let you add new methods to an existing class, much like C# extensions. However, do not confuse C# extensions with Objective-C extensions. Objective-C’s extensions are a special case of categories that let you define methods that must be declared in the main implementation block.

These are powerful features that have many potential uses. First, categories make it possible to split up a class’ interface and implementation into several files, which provides much-needed modularity for larger projects. Second, categories let you fix bugs in an existing class (e.g., NSString) without the need to subclass it. Third, they provide an effective alternative to the protected and private methods found in C# and other Simula-like languages.

category is a group of related methods for a class, and all of the methods defined in a category are available through the class as if they were defined in the main interface file. As an example, take the Person class that we’ve been working with throughout this book. If this were a large project, Person may have dozens of methods ranging from basic behaviors to interactions with other people to identity checking. The API might call for all of these methods to be available through a single class, but it’s much easier for developers to maintain if each group is stored in a separate file. In addition, categories eliminate the need to recompile the entire class every time you change a single method, which can be a time-saver for very large projects.

Let’s take a look at how categories can be used to achieve this. We start with a normal class interface and a corresponding implementation:

// Person.h
@interface Person : NSObject
@interface Person : NSObject
@property (readonly) NSMutableArray* friends;
@property (copy) NSString* name;
- (void)sayHello;
- (void)sayGoodbye;
// Person.m
#import "Person.h"
@implementation Person
@synthesize name = _name;
@synthesize friends = _friends;
    self = [super init];
        _friends = [[NSMutableArray alloc] init];
    return self;
- (void)sayHello {
    NSLog(@"Hello, says %@.", _name);
- (void)sayGoodbye {
    NSLog(@"Goodbye, says %@.", _name);

Nothing new here-just a Person class with two properties (the friends property will be used by our category) and two methods. Next, we’ll use a category to store some methods for interacting with other Person instances. Create a new file, but instead of a class, use the Objective-C Category template. Use Relations for the category name and Person for the Category on field:

Figure 28 Creating the PersonRelations class
Creating the Person+Relations class

As expected, this will create two files: a header to hold the interface and an implementation. However, these will both look slightly different than what we’ve been working with. First, let’s take a look at the interface:

// Person+Relations.h
#import <Foundation/Foundation.h>
#import "Person.h"
@interface Person (Relations)
- (void)addFriend:(Person *)aFriend;
- (void)removeFriend:(Person *)aFriend;
- (void)sayHelloToFriends;

Instead of the normal @interface declaration, we include the category name in parentheses after the class name we’re extending. A category name can be anything, as long as it doesn’t conflict with other categories for the same class. A category’s file name should be the class name followed by a plus sign, followed by the name of the category (e.g., Person+Relations.h ).

So, this defines our category’s interface. Any methods we add in here will be added to the original Person class at run time. It will appear as though the addFriend:removeFriend:, and sayHelloToFriends methods are all defined in Person.h, but we can keep our functionality encapsulated and maintainable. Also note that you must import the header for the original class, Person.h. The category implementation follows a similar pattern:

// Person+Relations.m
#import "Person+Relations.h"
@implementation Person (Relations)
- (void)addFriend:(Person *)aFriend {
    [[self friends] addObject:aFriend];
- (void)removeFriend:(Person *)aFriend {
    [[self friends] removeObject:aFriend];
- (void)sayHelloToFriends {
    for (Person *friend in [self friends]) {
        NSLog(@"Hello there, %@!", [friend name]);

This implements all of the methods in Person+Relations.h. Just like the category’s interface, the category name appears in parentheses after the class name. The category name in the implementation should match the one in the interface.

Also, note that there is no way to define additional properties or instance variables in a category. Categories have to refer back to data stored in the main class (friends in this instance).

It’s also possible to override the implementation contained in Person.m by simply redefining the method in Person+Relations.m. This can be used to monkey patch an existing class; however, it’s not recommended if you have an alternative solution to the problem, since there would be no way to override the implementation defined by the category. That is to say, unlike the class hierarchy, categories are a flat organizational structure-if you implement the same method in two separate categories, it’s impossible for the runtime to figure out which one to use.

The only change you have to make to use a category is to import the category’s header file. As you can see in the following example, the Person class has access to the methods defined in Person.h along with those defined in the category Person+Relations.h:

// main.m
#import <Foundation/Foundation.h>
#import "Person.h"
#import "Person+Relations.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Person *joe = [[Person alloc] init];
        joe.name = @"Joe";
        Person *bill = [[Person alloc] init];
        bill.name = @"Bill";
        Person *mary = [[Person alloc] init];
        mary.name = @"Mary";
        [joe sayHello];
        [joe addFriend:bill];
        [joe addFriend:mary];
        [joe sayHelloToFriends];
    return 0;

And that’s all there is to creating categories in Objective-C.

To reiterate, all Objective-C methods are public-there is no language construct to mark them as either private or protected. Instead of using “true” protected methods, Objective-C programs can combine categories with the interface/implementation paradigm to achieve the same result.

The idea is simple: declare “protected” methods as a category in a separate header file. This gives subclasses the ability to “opt-in” to the protected methods while unrelated classes use the “public” header file as usual. For example, take a standard Ship interface:

// Ship.h
#import <Foundation/Foundation.h>
@interface Ship : NSObject
- (void)shoot;

As we’ve seen many times, this defines a public method called shoot. To declare a protected method, we need to create a Ship category in a dedicated header file:

// Ship_Protected.h
#import <Foundation/Foundation.h>
@interface Ship(Protected)
- (void)prepareToShoot;

Any classes that need access to the protected methods (namely, the superclass and any subclasses) can simply import Ship_Protected.h. For example, the Ship implementation should define a default behavior for the protected method:

// Ship.m
#import "Ship.h"
#import "Ship_Protected.h"
@implementation Ship {
    BOOL _gunIsReady;
- (void)shoot {
    if (!_gunIsReady) {
        [self prepareToShoot];
        _gunIsReady = YES;
- (void)prepareToShoot {
    // Execute some private functionality.
    NSLog(@"Preparing the main weapon...");

Note that if we hadn’t imported Ship_Protected.h, this prepareToShoot implementation would be a private method, as discussed in the Methods chapter. Without a protected category, there would be no way for subclasses to access this method. Let’s subclass the Ship to see how this works. We’ll call it ResearchShip:

// ResearchShip.h
#import "Ship.h"
@interface ResearchShip : Ship
- (void)extendTelescope;

This is a normal subclass interface-it should not import the protected header, as this would make the protected methods available to anyone that imports ResearchShip.h, which is precisely what we’re trying to avoid. Finally, the implementation for the subclass imports the protected methods and (optionally) overrides them:

// ResearchShip.m
#import "ResearchShip.h"
#import "Ship_Protected.h"
@implementation ResearchShip
- (void)extendTelescope {
    NSLog(@"Extending the telescope");
// Override protected method
- (void)prepareToShoot {
    NSLog(@"Oh shoot! We need to find some weapons!");

To enforce the protected status of the methods in Ship_Protected.h, other classes aren’t allowed to import it. They’ll just import the normal “public” interfaces of the superclass and subclass:

// main.m
#import <Foundation/Foundation.h>
#import "Ship.h"
#import "ResearchShip.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Ship *genericShip = [[Ship alloc] init];
        [genericShip shoot];
        Ship *discoveryOne = [[ResearchShip alloc] init];
        [discoveryOne shoot];
    return 0;

Since neither main.mShip.h, nor ResearchShip.h import the protected methods, this code won’t have access to them. Try adding a [discoveryOne prepareToShoot] method-it will throw a compiler error, since the prepareToShoot declaration is nowhere to be found.

To summarize, protected methods can be emulated by placing them in a dedicated header file and importing that header file into the implementation files that require access to the protected methods. No other files should import the protected header.

While the workflow presented here is a completely valid organizational tool, keep in mind that Objective-C was never meant to support protected methods. Think of this as an alternative way to structure an Objective-C method, rather than a direct replacement for C#/Simula-style protected methods. It’s often better to look for another way to structure your classes rather than forcing your Objective-C code to act like a C# program.

One of the biggest issues with categories is that you can’t reliably override methods defined in categories for the same class. For example, if you defined an addFriend: class in Person(Relations) and later decided to change the addFriend: implementation via a Person(Security) category, there is no way for the runtime to know which method it should use since categories are, by definition, a flat organizational structure. For these kinds of cases, you need to revert to the traditional subclassing paradigm.

Also, it’s important to note that a category can’t add instance variables. This means you can’t declare new properties in a category, as they could only be synthesized in the main implementation. Additionally, while a category technically does have access to its classes’ instance variables, it’s better practice to access them through their public interface to shield the category from potential changes in the main implementation file.

Extensions (also called class extensions) are a special type of category that requires their methods to be defined in the main implementation block for the associated class, as opposed to an implementation defined in a category. This can be used to override publicly declared property attributes. For example, it is sometimes convenient to change a read-only property to a read-write property within a class’ implementation. Consider the normal interface for a Ship class:

Included code sample: Extensions

// Ship.h
#import <Foundation/Foundation.h>
#import "Person.h"
@interface Ship : NSObject
@property (strong, readonly) Person *captain;
- (id)initWithCaptain:(Person *)captain;

It’s possible to override the @property definition inside of a class extension. This gives you the opportunity to re-declare the property as readwrite in the implementation file. Syntactically, an extension looks like an empty category declaration:

// Ship.m
#import "Ship.h"
// The class extension.
@interface Ship()
@property (strong, readwrite) Person *captain;
// The standard implementation.
@implementation Ship
@synthesize captain = _captain;
- (id)initWithCaptain:(Person *)captain {
    self = [super init];
    if (self) {
        // This WILL work because of the extension.
        [self setCaptain:captain];
    return self;

Note the () appended to the class name after the @interface directive. This is what marks it as an extension rather than a normal interface or a category. Any properties or methods that appear in the extension must be declared in the main implementation block for the class. In this case, we aren’t adding any new fields-we’re overriding an existing one. But unlike categories, extensions can add extra instance variables to a class, which is why we’re able to declare properties in a class extension but not a category.

Because we re-declared the captain property with a readwrite attribute, the initWithCaptain: method can use the setCaptain: accessor on itself. If you were to delete the extension, the property would return to its read-only status and the compiler would complain. Clients using the Ship class aren’t supposed to import the implementation file, so the captain property will remain read-only.

#import <Foundation/Foundation.h>
#import "Person.h"
#import "Ship.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Person *heywood = [[Person alloc] init];
        heywood.name = @"Heywood";
        Ship *discoveryOne = [[Ship alloc] initWithCaptain:heywood];
        NSLog(@"%@", [discoveryOne captain].name);
        Person *dave = [[Person alloc] init];
        dave.name = @"Dave";
        // This will NOT work because the property is still read-only.
        [discoveryOne setCaptain:dave];
    return 0;

Another common use case for extensions is for declaring private methods. In the previous chapter, we saw how private methods can be declared by simply adding them anywhere in the implementation file. But, prior to Xcode 4.3, this was not the case. The canonical way to create a private method was to forward-declare it using a class extension. Let’s take a look at this by slightly altering the Ship header from the previous example:

// Ship.h
#import <Foundation/Foundation.h>
@interface Ship : NSObject
- (void)shoot;


Next, we’re going to recreate the example we used when we discussed private methods in the Methods chapter. Instead of simply adding the private prepareToShoot method to the implementation, we need to forward-declare it in a class extension.

// Ship.m
#import "Ship.h"
// The class extension.
@interface Ship()
- (void)prepareToShoot;
// The rest of the implementation.
@implementation Ship {
    BOOL _gunIsReady;
- (void)shoot {
    if (!_gunIsReady) {
        [self prepareToShoot];
        _gunIsReady = YES;
- (void)prepareToShoot {
    // Execute some private functionality.
    NSLog(@"Preparing the main weapon...");

The compiler ensures the extension methods are implemented in the main implementation block, which is why it functions as a forward-declaration. Yet because the extension is encapsulated in the implementation file, other objects shouldn’t ever know about it, giving us another way to emulate private methods. While newer compilers save you this trouble, it’s still important to understand how class extensions work, as it has been a common way to leverage private methods in Objective-C programs until very recently.

This chapter covered two of the more unique concepts in the Objective-C programming language: categories and extensions. Categories are a way to extend the API of existing classes, and extensions are a way to add required methods to the API outside of the main interface file. Both of these were initially designed to ease the burden of maintaining large code bases.

The next chapter continues our journey through Objective-C’s organizational structures. We’ll learn how to define a protocol, which is an interface that can be implemented by a variety of classes.

xcode下已经使用 “Automatically manage signing”,还提示“is automatically signed, but provisioning profile 1fe1af09-c71e-4300-94d8-76d14cec2eb4 has been manually specified. Set the provisioning profile value to “Automatic” in the build settings editor, or switch to manual signing in the Signing & Capabilities editor.”解决方案

 开发  xcode下已经使用 “Automatically manage signing”,还提示“is automatically signed, but provisioning profile 1fe1af09-c71e-4300-94d8-76d14cec2eb4 has been manually specified. Set the provisioning profile value to “Automatic” in the build settings editor, or switch to manual signing in the Signing & Capabilities editor.”解决方案已关闭评论
12月 092019

xcode项目签名已经使用 “Automatically manage signing”,但还是提示如下:

XXXXXX is automatically signed, but provisioning profile 1fe1af09-c71e-4300-94d8-76d14cec2eb4 has been manually specified. Set the provisioning profile value to “Automatic” in the build settings editor, or switch to manual signing in the Signing & Capabilities editor.



  1.  右键点击 项目文件  xxxx.xcodeproj –> 显示包内容(Show Package Contents)–> 打开并编辑 project.pbxproj
  2. 在project.pbxproj 搜索“1fe1af09-c71e-4300-94d8-76d14cec2eb4”,找到对应的行,删除
  3. 再次进入xcode,使用“Automatically manage signing”,Done!


 mac  MacOS下命令行查询网络端口占用情况已关闭评论
12月 052019


1. netstat命令
[[email protected] centos]# netstat -an | grep 8889
tcp 0 0* LISTEN
tcp6 0 0 :::8889 :::* LISTEN

但是 OS X 的 netstat 不能查看使用端口的程序名,建议使用 lsof 代替 netstat 进行查看

通过lsof(list open file)命令可以查看到当前打开文件,在linux(mac os内核也基于unix)中所有事物都是以文件形式存在,包括网络连接及硬件设备。
-n 表示不显示主机名
-P 表示不显示端口俗称

[[email protected] centos]# lsof -i:8889 -P -n
python 24433 root 4u IPv6 146243463 0t0 TCP *:8889 (LISTEN)
python 24433 root 6u IPv4 146243464 0t0 TCP *:8889 (LISTEN)


sudo lsof -i -P | grep -i “listen”
sudo lsof -nP -iTCP -sTCP:LISTEN

利用mac自带QuickTime Player录制mac内部声音

 mac  利用mac自带QuickTime Player录制mac内部声音已关闭评论
12月 032019

前段时间想要录制下网页版微信公众号程序里的英文学习音频,我也不需要编辑等复杂功能,发现使用quicktime + 第三方插件soundflower即可, 插入耳机也可以录入声音,终于不用开扬声器也能录了。

Mac中QuickTime Player原生自带的功能里面不带有录制内部声音的功能,而是由内置麦克风录制外界声音。因此,想要录制高质量的电脑内部音源用macOS本身自带功能是捉襟见肘的。这个时候就需要借助像「soundflower」这样的第三方插件来实现我们的需求。



下载:Soundflower下载(提取码:htr7 )。按照下图安装。









配置好之后就可以打开QuickTime Player了,到屏幕左上角-【文件】-【新建音频录制】。打开之后如下图,电脑外放或者耳机有声音输入时,可以看见电平的波动,这个时候点击录制按钮就可以了。QuickTime Player界面最下面一行请把音量调到零,否则录音会有回音。


录制完后点停止按钮,并通过quicktime菜单  【文件】– > 【存储为…】 保存音频文件即可





 代理  mitmproxy/mitmdump/mitmweb抓取websocket包并解码输出已关闭评论
11月 292019



mitmproxy/mitmdump/mitmweb命令行支持 -s  脚本文件.py,此脚本文件(addons)支持的Events事件见



mitmproxy 自带的addons实例文件可参考:https://github.com/mitmproxy/mitmproxy/tree/master/mitmproxy/addons




#!mitmdump -s

import mitmproxy.addonmanager

import mitmproxy.connections

import mitmproxy.http

import mitmproxy.log

import mitmproxy.tcp

import mitmproxy.websocket

import mitmproxy.proxy.protocol

class SniffWebSocket:

    def __init__(self):


    # Websocket lifecycle

    def websocket_handshake(self, flow: mitmproxy.http.HTTPFlow):


            Called when a client wants to establish a WebSocket connection. The

            WebSocket-specific headers can be manipulated to alter the

            handshake. The flow object is guaranteed to have a non-None request



    def websocket_start(self, flow: mitmproxy.websocket.WebSocketFlow):


            A websocket connection has commenced.


    def websocket_message(self, flow: mitmproxy.websocket.WebSocketFlow):


            Called when a WebSocket message is received from the client or

            server. The most recent message will be flow.messages[-1]. The

            message is user-modifiable. Currently there are two types of

            messages, corresponding to the BINARY and TEXT frame types.


        for flow_msg in flow.messages:

            packet = flow_msg.content

            from_client = flow_msg.from_client

            print(“[” + (“Sended” if from_client else “Reveived”) + “]: decode the packet here: %r…” % packet)

    def websocket_error(self, flow: mitmproxy.websocket.WebSocketFlow):


            A websocket connection has had an error.


        print(“websocket_error, %r” % flow)

    def websocket_end(self, flow: mitmproxy.websocket.WebSocketFlow):


            A websocket connection has ended.


addons = [




使用命令行 mitmproxy或mitmdump或mitmweb 加 -s snifferWS.py(如: mitmweb -s snifferWS.py) 即可以看到websocket解码的输出了.

实际使用中我一般使用mitmweb -s snifferWS.py 方式,因为这样在web浏览器里可以窗口化显示http/https的详细输入输出,而在终端运行的窗口可以看到websocket的信息。



在使用中发现在我的ios 13.1的iphone机器上无法抓到websocket的解码内容,但android的机器可以,目前还没有找到好的方法,有知道的朋友可以分享下


 开发  mitmproxy支持https抓包已关闭评论
11月 292019


  1. 先运行mitmproxy或mitmdump或mitmweb启动代理服务器后(默认8080端口), 在手机里设置好代理服务器。
  2. 打开浏览器,并浏览地址: http://mitm.it, 可以看到下面的画面:选择对应系统的证书文件下载。
  3. 如果是ios用户,记得在描述文件下载后安装。



python输出有颜色和背景色的文字(\033[显示方式;前景色;背景色m, \033[0m )

 python  python输出有颜色和背景色的文字(\033[显示方式;前景色;背景色m, \033[0m )已关闭评论
11月 212019


设置颜色开始 :\033[显示方式;前景色;背景色m

\033[31;43;1m #—红底黄字高亮显示
\033[0m     #—采用终端默认设置,即缺省颜色,一般可以在文字显示后,添加在末尾以恢复默认

30 黑色
31  红色
32 绿色
33 黃色
34 蓝色
35 紫红色
36 青蓝色
37 白色

40 黑色
41 红色
42 绿色
43 黃色
44 蓝色
45 紫红色
46 青蓝色
47 白色


0 终端默认设置
1 高亮显示
4 使用下划线
5 闪烁
7 反白显示


大家可以发现: ‘;’前后的三个参数:显示方式,前景色,背景色是可选参数,可以只写其中的某一个或者某两个;由于表示三个参数不同含义的数值都是唯一没有重复的,所以三个参数的书写先后顺序没有固定要求,系统都可识别,所以参数的前后顺序实际是没有关系,但还是建议大家按规则来。




print("\033[1;33m 我是1;33m的显示效果   \033[3;31m")
print('我是是上一行结尾的3;31m颜色输出效果 ')

print("\033[30;1mHello, Welcome To Python! " + "#黑字")   #黑
print("\033[31;1mHello, Welcome To Python! " + "#红字")   #红
print("\033[32;1mHello, Welcome To Python! " + "#绿字")   #绿
print("\033[33;1mHello, Welcome To Python! " + "#黄字")   #黄
print("\033[34;1mHello, Welcome To Python! \033[0m" + "#蓝字")   #蓝
print("\033[35;1mHello, Welcome To Python! \033[0m" + "#紫字")   #紫
print("\033[36;1mHello, Welcome To Python! \033[0m" + "#青字") #青
print("\033[37;1mHello, Welcome To Python! \033[0m" + "#白字") #白
print("\033[40;1mHello, Welcome To Python! \033[0m" + "#黑底")   #黑
print("\033[41;1mHello, Welcome To Python! " + "#红底")   #红
print("\033[42;1mHello, Welcome To Python! " + "#绿底")   #绿
print("\033[43;1mHello, Welcome To Python! \033[0m" + "#黄底")   #黄
print("\033[44;1mHello, Welcome To Python! \033[0m" +"#蓝底")   #蓝
print("\033[45;1mHello, Welcome To Python! \033[0m" + "#紫底")   #紫
print("\033[46;1mHello, Welcome To Python! \033[0m" + "#青底")   #青
print("\033[47;1mHello, Welcome To Python! \033[0m" + "#白底")   #白
print("\033[31;43;1mHello, Welcome To Python! \033[0m" + "#红字黄底")   #红字黄底
print("\033[43;31;1mHello, Welcome To Python! \033[0m" + "#红字黄底(有意调换)")   #红字黄底
print("\033[35;42;1mHello, Welcome To Python! \033[0m" + "#紫字绿底")   #紫字绿底
print("\033[30;47;1mHello, Welcome To Python! \033[0m" + "#高亮显示")   #高亮显示
print("\033[30;47;4mHello, Welcome To Python! \033[0m" + "#使用下划线")   #使用下划线


Mac下 跳到行尾、行首、Home、End、Page up、Page down、Delete、Backward、Backspace、Forward、等常用快捷键

 mac  Mac下 跳到行尾、行首、Home、End、Page up、Page down、Delete、Backward、Backspace、Forward、等常用快捷键已关闭评论
11月 202019


Ctrl+u: 删除光标之前到行首的字符
fn+上方向键是page up
fn+下方向键是page down
Ctrl + d : 删除一个字符,相当于通常的Delete键
Ctrl + h退格删除一个字符,相当于通常的Backspace键
Ctrl + f光标向前(Forward)移动一个字符位置
Ctrl + b光标往回(Backward)移动一个字符位置




[PM2] Spawning PM2 daemon with pm2_home=/home/ec2-user/.pm2问题排查

 Nodejs, pm2  [PM2] Spawning PM2 daemon with pm2_home=/home/ec2-user/.pm2问题排查已关闭评论
11月 192019

新aws机器部署了老nodejs项目,使用pm2 start config.js启动,提示:

[PM2] Spawning PM2 daemon with pm2_home=/home/ec2-user/.pm2


没有错误提示,pm2 log  / pm2 status都看不到信息, nodejs项目也没启动。


其实可以通过查看~/.pm2/pm2.log 文件获取真正错误信息:

SyntaxError: Unexpected identifier
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:549:28)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
at Module.require (module.js:504:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/usr/local/lib/nodejs/lib/node_modules/pm2/lib/Watcher.js:6:16)
/home/ec2-user/.pm2/pm2.log (END)
async remove(item) {


以为是chokidar的版本太新了,切换到[email protected],再运行pm2 start config.js还是上面的错误。 由于nodejs使用的是比较老的6.15.1的版本,想到会不会是pm2的版本太新了,因此,将pm2由4.1.2降为3.2.3

npm insall -g [email protected]


安装完后果然OK了, 记录下希望对其他也碰到此类问题的有一些帮助。

/etc/profile 添加的环境变量切换到root下环境变量无效的解决方法

 linux  /etc/profile 添加的环境变量切换到root下环境变量无效的解决方法已关闭评论
11月 192019

/etc/profile 添加的环境变量切换到root下环境变量无效的解决方法:



export NODE_HOME=/usr/local/lib/nodejs
export PATH=${NODE_HOME}/bin:$PATH


$ . /etc/bashrc