首页 \ 问答 \ 来自不同类的NSMutableArray获取Null(NSMutableArray from different class gets Null)

来自不同类的NSMutableArray获取Null(NSMutableArray from different class gets Null)

我上过这堂课:

@interface PersonModel : NSObject

@property (nonatomic, weak) NSString *string;
@property (nonatomic, weak) NSMutableArray *array;

@end

在另一个类中,我使用该字符串和数组。 字符串很好,但数组变为null。 我像往常一样启动它,像这样:

person.array = [[NSMutableArray alloc] init];
[person.array addObject:[object copy]];
NSLog(@"Array: %@", person.array);

I've this class:

@interface PersonModel : NSObject

@property (nonatomic, weak) NSString *string;
@property (nonatomic, weak) NSMutableArray *array;

@end

and in another class, I use that string and array. The string goes fine, but the array getting null. I initiate it as usual, like this:

person.array = [[NSMutableArray alloc] init];
[person.array addObject:[object copy]];
NSLog(@"Array: %@", person.array);

原文:https://stackoverflow.com/questions/13990265
更新时间:2022-09-19 12:09

最满意答案

您可以使用short_open_tag ,它必须在您的配置中启用,但这不是一个好习惯,因为它只有在启用时才有效 - 并且它们并不总是(默认情况下可能不是)

使用长标签和回声/打印可能会更长,是的...但我建议使用那些,而不是短标签。


另请注意,当数据来自不受信任的来源和/或可能包含您不希望在页面中注入的HTML时,您可能需要转义数据,以避免注入HTML / JS (请参阅htmlspecialchars


在评论之后编辑,添加关于short_open_tag的一些内容:

为什么考虑短开标签(至少是我^^)不良做法?

首先,经过一些检查后,默认情况下不会启用它们:

对于PHP 5.3:

squale@shark:~/temp/php/php-5.3.0
$ grep 'short_open_tag' php.ini-development
; short_open_tag
short_open_tag = Off
squale@shark:~/temp/php/php-5.3.0
$ grep 'short_open_tag' php.ini-production
; short_open_tag
short_open_tag = Off

默认情况下在“ development ”或“ production ”设置中禁用。

对于PHP 5.2.10 (最新版本的PHP 5.2)

squale@shark:~/temp/php/php-5.2.10
$ grep 'short_open_tag' php.ini-dist
short_open_tag = On
squale@shark:~/temp/php/php-5.2.10
$ grep 'short_open_tag' php.ini-recommended
; - short_open_tag = Off           [Portability]
short_open_tag = Off

默认情况下在“ recommended ”设置中禁用


考虑到这些默认设置有时(通常是?)由托管服务保留,依赖于short_open_tag被激活是危险的。
(我自己遇到了被禁用的问题......当你不是服务器的管理员并且没有必要的特权来修改它时,它并不好玩^^)

如果你想要一些数字,你可以看看Quick survery:默认情况下打开或关闭short_open_tag支持?
(不是科学证明 - 但表明将它们用于您向公众发布的应用程序可能很危险)


就像你说的那样,那些在激活时会与XML声明冲突 - 意味着你必须使用这样的东西:

<?php echo '<?xml version="1.0" encoding="UTF-8" ?>'; ?>

考虑到存在短暂打开的标签,并且可能会在您将使用的服务器上激活,但您可能不会使用<?xml ; 太糟糕了:-(


实际上,阅读PHP 5.2.10推荐的php.ini:

; Allow the <? tag.  Otherwise, only <?php and <script> tags are recognized.
; NOTE: Using short tags should be avoided when developing applications or
; libraries that are meant for redistribution, or deployment on PHP
; servers which are not under your control, because short tags may not
; be supported on the target server. For portable, redistributable code,
; be sure not to use short tags.

PHP 6中的那个更有趣:

; This directive determines whether or not PHP will recognize code between
; <? and ?> tags as PHP source which should be processed as such. It's been
; recommended for several years that you not use the short tag "short cut" and
; instead to use the full <?php and ?> tag combination. With the wide spread use
; of XML and use of these tags by other languages, the server can become easily
; confused and end up parsing the wrong code in the wrong context. But because
; this short cut has been a feature for such a long time, it's currently still
; supported for backwards compatibility, but we recommend you don't use them.

(在PHP 5.3中可能是相同的;没有检查)


传言说可以从PHP 6中删除短开标签; 考虑到我刚刚发布的php.ini部分,它可能不会...但是,仍然......


给出一个指向另一个方向的参数(毕竟我必须诚实): 对模板文件使用短开放标签(仅限)通常在Zend Framework的使用模板文件的示例中完成:

在我们的示例和文档中,我们使用PHP短标记:

也就是说,许多开发人员更喜欢使用完整标签来验证或移植。 例如,php.ini.recommended文件中禁用了short_open_tag,如果在视图脚本中模板化XML,则短打开标记将导致模板验证失败。

来源

相反,对于.php文件:

永远不允许使用短标签。 对于仅包含PHP代码的文件,必须始终省略结束标记

来源


我希望这些信息是有用的,并为你的评论带来某种答案:-)


You could use short_open_tag, which have to be enabled in your configuration, but that's not considered as a good practice, as it only works if those are enabled -- and they are not always (maybe not even by default)

Using long tags and echo/print might be longer, yes... But I would recommend using those, and not short tags.


Also note that you might need to escape your data, when it comes from an un-trusted source and/or might contain HTML you don't want to get injected in the page, to avoid injections of HTML/JS (see htmlspecialchars) :


EDIT after the comments, to add couple of things about short_open_tag :

Why are short open tags considered (at least by me ^^ ) bad practice ?

First of all, after some checking, they are not enabled by default :

For PHP 5.3 :

squale@shark:~/temp/php/php-5.3.0
$ grep 'short_open_tag' php.ini-development
; short_open_tag
short_open_tag = Off
squale@shark:~/temp/php/php-5.3.0
$ grep 'short_open_tag' php.ini-production
; short_open_tag
short_open_tag = Off

Disabled by default in either "development" or "production" settings.

For PHP 5.2.10 (most recent version of PHP 5.2) :

squale@shark:~/temp/php/php-5.2.10
$ grep 'short_open_tag' php.ini-dist
short_open_tag = On
squale@shark:~/temp/php/php-5.2.10
$ grep 'short_open_tag' php.ini-recommended
; - short_open_tag = Off           [Portability]
short_open_tag = Off

Disabled by default in the "recommended" settings


Considering these default settings are sometimes (often ?) kept by hosting services, it is dangerous to rely on short_open_tag being activated.
(I have myself run into problem with those being disabled... And when you are not admin of the server and don't have required privilegies to modify that, it's not fun ^^ )

If you want some numbers, you can take a look at Quick survery: short_open_tag support on or off by default?
(Not a scientific proof -- but show it could be dangerous to use those for an application you'd release to the public)


Like you said, those, when activated, conflict with XML declaration -- means you have to use something like this :

<?php echo '<?xml version="1.0" encoding="UTF-8" ?>'; ?>

Considering short open tags exists, and might be activated on the server you'll use, you should probable not use <?xml ever, though ; too bad :-(


Actually, reading through the php.ini-recommended of PHP 5.2.10 :

; Allow the <? tag.  Otherwise, only <?php and <script> tags are recognized.
; NOTE: Using short tags should be avoided when developing applications or
; libraries that are meant for redistribution, or deployment on PHP
; servers which are not under your control, because short tags may not
; be supported on the target server. For portable, redistributable code,
; be sure not to use short tags.

The one from PHP 6 is even more interesting :

; This directive determines whether or not PHP will recognize code between
; <? and ?> tags as PHP source which should be processed as such. It's been
; recommended for several years that you not use the short tag "short cut" and
; instead to use the full <?php and ?> tag combination. With the wide spread use
; of XML and use of these tags by other languages, the server can become easily
; confused and end up parsing the wrong code in the wrong context. But because
; this short cut has been a feature for such a long time, it's currently still
; supported for backwards compatibility, but we recommend you don't use them.

(Might be the same in PHP 5.3 ; didn't check)


There have been rumors short open tags could be removed from PHP 6 ; considering the portion of php.ini I just posted, it probably won't... but, still...


To give an argument pointing to the other direction (I've gotta be honest, after all) : using short open tags for template files (only) is something that is often done in Zend Framework's examples that use template files :

In our examples and documentation, we make use of PHP short tags:

That said, many developers prefer to use full tags for purposes of validation or portability. For instance, short_open_tag is disabled in the php.ini.recommended file, and if you template XML in view scripts, short open tags will cause the templates to fail validation.

(source)

On the contrary, for .php files :

Short tags are never allowed. For files containing only PHP code, the closing tag must always be omitted

(source)


I hope those informations are useful, and bring some kind of answer to your comment :-)

相关问答

更多

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)