来自不同类的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 :-)

相关问答

更多

为什么PHP在mysql表数据库中插入变量名[关闭](Why is PHP inserting variable name in mysql table database [closed])

$name = mysql_escape_string($_POST['name']); $lname = mysql_escape_string($_POST['lname']); $uname = mysql_escape_string($_POST['uname']); $email1 = mysql_escape_string($email1); $email2 = mysql_...

使用PHP将变量插入文本的最短方法是什么?(What is the shortest way of inserting a variable into text with PHP?)

您可以使用short_open_tag ,它必须在您的配置中启用,但这不是一个好习惯,因为它只有在启用时才有效 - 并且它们并不总是(默认情况下可能不是) 使用长标签和回声/打印可能会更长,是的...但我建议使用那些,而不是短标签。 另请注意,当数据来自不受信任的来源和/或可能包含您不希望在页面中注入的HTML时,您可能需要转义数据,以避免注入HTML / JS (请参阅htmlspecialchars ) : 在评论之后编辑,添加关于short_open_tag的一些内容: 为什么考虑短开标签(...

需要帮助将默认文本值插入mysql(need help inserting a default text value into mysql)

使用默认值的一种非常简单的方法可能是: $photo = isset($photo) ? $photo : 'images/avatarDefault.png' ; 它的工作原理是它首先询问是否设置了照片,如果是,则使用所有准备插入的值,否则插入默认值, 另一种(非常相似)方法: $photo = !empty($photo) ? $photo : 'images/avatarDefault.png' ; UPDATE 检查它是否包含某个“扩展名”将是一个简单的重写 $photo = preg...

在文件之间传递PHP变量并将变量插入SQL表(Passing PHP variable between files and inserting the variable into a SQL table)

尝试 <input type="hidden" name="blog_id" value="<?php echo $thisID; ?>"> 因为您已将get值存储在变量$thisID 如果你想发送获取数据然后使用 <input type="hidden" name="blog_id" value="<?php echo $_GET['blog_id']; ?>"> try <input type="hidden" name="blog_id" value="<?php echo $thisI...

使用带AJAX的PHP将JS变量插入MySQL表的麻烦(Trouble with inserting JS variable into MySQL table with PHP with AJAX)

尝试编写像这样的Ajax数据参数 data: { 'paramdata':paramdata } 此外,您从未真正查询过您的数据。 mysqli_query($mysqli, $sql); 但是由于你得到的错误,可能是因为ajax数据参数。 Try writing your Ajax data parameters like this data: { 'paramdata':paramdata } Also, you never actually queried your d...

在Javascript文件中插入PHP变量值(Inserting PHP variable value in Javascript file)

而不是尝试将PHP混合到JavaScript文件中,下面是我在HTML模板中做的事情: <head> <script> var BASEURL = "<?php echo base_url(); ?>"; </script> <script src="/path/to/my_script1.js"></script> <script src="/path/to/my_script2.js"></script> </head> 这设置变量并允许所有嵌...

需要PHP和MySQL的帮助(Need help with PHP and MySQL)

list($min) = mysql_fetch_row(mysql_query('SELECT IFNULL(MIN(column), 0) FROM table')); 如果你想在没有行时返回0以外的其他东西,只需编辑IFNULL的第二个参数。 如果确保至少有一行,或者在没有行时想要NULL,则可以完全删除IFNULL 。 list($min) = mysql_fetch_row(mysql_query('SELECT IFNULL(MIN(column), 0) FROM table'))...

将数据插入mysql db时,Null Php变量(Null Php Variable when inserting data into mysql db)

如果您正在使用邮递员并且您正在发布数据,则需要确保“card_number”是您的密钥,您想要的任何值都是您的价值。 看起来像发布“card_number”作为密钥“card_number”的值 编辑:在查看截图后,您需要将参数放在BODY“form-data”中。 你现在把这些参数放在网址中。 If you are using postman and you are POSTing data, you need to make sure that "card_number" is your ke...

插入数据库我的sql php(Inserting into database my sql php)

您在定义变量之前创建SQL查询。 <?php require_once("conn.php"); if (isset($_REQUEST["submit"])) { if (isset($_POST["submit"])) { $firstname = $_POST["gname"]; $middlename = $_POST["mname"]; $lastname = $_POST["surname"]; $user = $_POST["u...

Fibonacci序列 - PHP最短代码(Fibonacci Sequence - PHP shortest code possible)

对最短代码的研究被称为“代码高尔夫”,并且有一个专门用于它的整个堆栈交换站点 。 特别是,您的问题在这里得到解答。 代码是: <?for($a=$b++;;$b+=$a=$b-$a){echo$a;} 这适用于: 删除空格(无论如何都被忽略)(化妆品) 给变量无意义的名字(不影响输出)(化妆品) 滥用各种语言功能,如隐式初始化为零和多个赋值 多重赋值技巧允许您使用两个变量而不是三个(不需要“当前数字”)来利用赋值的顺序 The research for the shortest code is ...

相关文章

更多

【原】storm源码之一个class解决nimbus单点问题

一、storm nimbus 单节点问题概述 1、storm集群在生产环境部署之后,通常会是如下的结构 ...

Hibernate创建sessionFactory null

请问各位: 我在使用junit进行单元测试的时候,到实例化SessionFactory的时候不成功, ...

mybatis There is no getter for property named 'xx' in 'class java.lang.String

用mybatis查询时,传入一个字符串传参数,且进行判断时,会报 There is no get ...

Guava学习笔记:Optional优雅的使用null

  在我们学习和使用Guava的Optional之前,我们需要来了解一下Java中null。因为,只有 ...

Guava Optional类详解-处理null值

abstract T get()返回所包含的实例

Solr Document [null] missing required field: id 的原因

在solr建立索引的时候,如果你提交的doc中没有 id 这个Field,结果Solr在建立索引时候出 ...

List用完后需要赋null吗

从数据库查询N条记录放在List集合中,然后通过request对象返回给页面,通过循环遍历将List中 ...

如何将sql数据库值默认为空而不是null

如何将sql数据库值默认为空而不是null 问题补充: amos_tl 写道 ...

null 写“==”前和写“==”有何区别

if(null == bodyParas.getModifyData()){} 和if(bodyPar ...

DbUtil查找数据,查找出来的结果最后一列是null?

package com.webchart.dao; import java.sql.SQLExcep ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本