如何声明NSString常量以传递给NSNotificationCenter [英] How to declare NSString constants for passing to NSNotificationCenter

查看:113
本文介绍了如何声明NSString常量以传递给NSNotificationCenter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的.h档案中有以下内容:

I've got the following in my .h file:

#ifndef _BALANCE_NOTIFICATION
#define _BALANCE NOTIFICATION
const NSString *BalanceUpdateNotification
#endif

以及我的.m中的以下内容file:

and the following in my .m file:

const NSString *BalanceUpdateNotification = @"BalanceUpdateNotification";

我正在使用以下代码:

[[NSNotificationCenter defaultCenter]
    addObserver:self
    selector:@selector(updateBalance:)
    name:BalanceUpdateNotification
    object:nil];

[[NSNotificatoinCenter defaultCenter]
    postNotificationName:BalanceUpdateNotification
    object:self userInfo:nil];

哪个有效,但它给了我一个警告:

Which works, but it gives me a warning:

Passing argument 1 of 'postNotificationName:object:userInfo' discards qualifiers from pointer target type

所以,我可以将它转换为(NSString *),但我想知道这样做的正确方法是什么。

So, I can cast it to (NSString *), but I'm wondering what the proper way to do this is.

推荐答案

NSStrings 是不可变的,因此声明 const NSString * 将是多余的;只需使用 NSString *

NSStrings are immutable, so declaring a const NSString * would be redundant; just use NSString *.

如果您要做的是声明指针本身不能改变,那将是:

If what you're trying to do is declare that the pointer itself can't change, that would be:

   NSString * const BalanceUpdateNotification = @"BalanceUpdateNotification";

另请参阅
Objective-C中的常数

这篇关于如何声明NSString常量以传递给NSNotificationCenter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆