我的.h文件中有以下内容:
#ifndef _BALANCE_NOTIFICATION
#define _BALANCE NOTIFICATION
const NSString *BalanceUpdateNotification
#endif
Run Code Online (Sandbox Code Playgroud)
以及我的.m文件中的以下内容:
const NSString *BalanceUpdateNotification = @"BalanceUpdateNotification";
Run Code Online (Sandbox Code Playgroud)
我使用以下代码:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(updateBalance:)
name:BalanceUpdateNotification
object:nil];
Run Code Online (Sandbox Code Playgroud)
和
[[NSNotificatoinCenter defaultCenter]
postNotificationName:BalanceUpdateNotification
object:self userInfo:nil];
Run Code Online (Sandbox Code Playgroud)
哪个有效,但它给了我一个警告:
Passing argument 1 of 'postNotificationName:object:userInfo' discards qualifiers from pointer target type
Run Code Online (Sandbox Code Playgroud)
所以,我可以将其转换为(NSString*),但我想知道这样做的正确方法是什么.
dre*_*lax 32
通常,您extern
在标头中声明变量.最惯用的方式似乎是这样的:
#ifndef __HEADER_H__
#define __HEADER_H__
extern NSString * const BalanceUpdateNotification;
#endif
Run Code Online (Sandbox Code Playgroud)
#include "header.h"
NSString * const BalanceUpdateNotification = @"BalanceUpdateNotification";
Run Code Online (Sandbox Code Playgroud)
extern
告诉编译器某个类型NSString * const
的名称BalanceUpdateNotification
存在于某处.它可能在包含标题的源文件中,但可能不是.确保它确实存在并不是编译器的工作,只是根据您输入的方式适当地使用它.确保BalanceUpdateNotification
实际已在某处定义并且仅定义一次是连接器的工作.
把const
后面的*
手段你不能重新分配BalanceUpdateNotification
指向一个不同的NSString
.
Dav*_*har 24
NSStrings
是不可改变的,所以声明一个const NSString *
是多余的; 只是用NSString *
.
如果你要做的是声明指针本身不能改变,那将是:
NSString * const BalanceUpdateNotification = @"BalanceUpdateNotification";
Run Code Online (Sandbox Code Playgroud)
另请参见 Objective-C中的常量
归档时间: |
|
查看次数: |
19799 次 |
最近记录: |