为什么UPDATE语句适用于initWithFormat而NOT与stringWithFormat一起使用? [英] Why UPDATE statement works with initWithFormat and NOT with stringWithFormat?

查看:111
本文介绍了为什么UPDATE语句适用于initWithFormat而NOT与stringWithFormat一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UPDATE语句中遇到问题,正如我在此处所述:使用sqliteManager更新问题

I was having an issue with my UPDATE statement as I was telling here: Update issue with sqliteManager

我发现initWithFormat工作

I found out that initWithFormat WORKS

NSString *sqlStr = [[NSString alloc] initWithFormat:@"UPDATE User SET Name = :Name WHERE Id = :Id"];

但不是stringWithFormat:

BUT not stringWithFormat:

 NSString* sqlStr = [NSString stringWithFormat:@"UPDATE User SET Name = :Name WHERE Id = :Id"];

为什么会这样?我想了解背后的逻辑/推理..

Why is this as such? I would like to understand the logic/reasoning behind..

推荐答案

我刚从这个帖子中找到了一些有趣的东西:如何在NSTimer循环期间刷新TableView Cell数据

I have just found something interesting from this thread: How to refresh TableView Cell data during an NSTimer loop

我相信这是推理的原因..

This, I believe, is the reasoning behind..

我引用了什么petergb说:

I quote what "petergb" said:


[NSString stringWithFormat:...] 返回一个自动释放的对象。控制从程序代码返回到Apple提供的运行循环代码后,自动释放的对象将被释放。它们或多或少都是一种便利,所以我们不必释放我们在这里和那里使用过一次或两次的所有小物件。 (例如,想象一下,如果必须释放使用@语法创建的每个字符串,那将是多么乏味......)

[NSString stringWithFormat:...] returns an autoreleased object. Autoreleased objects get released after control returns from the program's code to the apple-supplied run-loop code. They are more or less a convenience so we don't have to release all the little objects that we use once or twice here and there. (For example, imagine how tedious it would be if you had to release every string you created with the @"" syntax...)

我们可以告诉 stringWithFormat :返回一个自动释放的对象,因为按照惯例,名称不以alloc或copy开头的方法总是返回自动释放的对象。据说这样的方法是抛售一个物体。我们可以在不久的将来使用这些对象,但我们并不拥有它(即我们不能指望它在我们将控制权返回给系统后就在那里。)如果我们想取得一个售卖对象的所有权,我们必须在它上面调用[object retain],然后它会在那里直到我们显式调用[object release]或[object autorelease],如果我们在失去对它的引用之前不调用release或autorelease通过将变量更改为其他内容,我们将泄漏它。

We can tell stringWithFormat: returns an autoreleased object because, by convention, methods who's names don't start with alloc or copy always return auto-released objects. Methods like this are said to "vend" an object. We can use these objects in the immediate future, but we don't "own" it (i.e. we can't count on it being there after we return control to the system.) If we want to take ownership of a vended object, we have to call [object retain] on it, and then it will be there until we explicitly call [object release] or [object autorelease], and if we don't call release or autorelease on it before we lose our reference to it by changing the variable to something else, we will leak it.

[[NSString alloc] initWithFormat:对比。此方法创建一个对象。我们拥有它。同样,它会在那里显式调用[object release]。

Contrast with [[NSString alloc] initWithFormat:. This method "creates" an object. We own it. Again, it will be there until we explicitly call [object release].

这篇关于为什么UPDATE语句适用于initWithFormat而NOT与stringWithFormat一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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