在阅读了对Objective C中关于单例的问题的回答之后,似乎每个解决方案都在实例访问器中对线程进行了一些权衡.即
@synchronized(self)
{
if (sharedInstance == nil)
sharedInstance = [[MySingleton alloc] init];
}
return sharedInstance;
Run Code Online (Sandbox Code Playgroud)
这基本上是单线程访问单例,如果它是在操作中经常使用的东西,看起来像是可能导致线程不必要地竞争的东西.
简单地使用类对象作为单例实例,并通过类方法公开功能,即
@interface MySingleton : NSObject {
}
+ (void)doSomething;
@end
@implementation MySingleton
+ (void)initialize {
//do some setup if necessary
}
+ (void)doSomething {
//do something
}
@end
Run Code Online (Sandbox Code Playgroud)
通过这种方式,我们避免每次想要引用单例对象时都进行锁定+检查,我们也可以省去将其存储在本地或方法ivar中.
此方法还允许运行时保证在任何给定时间系统中只存在一个实例(Class对象).
编辑
这里除了线程之外还有更多,使用传统的单例,您通常会编写如下代码:
MySingleton *instance = [MySingleton getSharedInstance];
NSObject *someResult = [instance getResult];
//or
if (instance.someProperty) {
//do something
}
Run Code Online (Sandbox Code Playgroud)
但是,如果你的单例是一个类实例,你基本上就不需要一直调用getSharedInstance.考虑以下代码:
NSObject *someResult = [MySingleton getResult];
//or
if ([MySingleton someProperty]) {
//do …Run Code Online (Sandbox Code Playgroud) 可能重复:
在字符串中包含常量而不连接
你好
我看到你可以像这样在字符串中调用调用变量:$X = "bla bla $variable";
我是否也可以使用常量来做这个,如果可以的话,什么是synthax?
当我们迁移到office 2010-64位版本时,我发现了以下函数调用的问题.
Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Run Code Online (Sandbox Code Playgroud)
根据http://msdn.microsoft.com/en-us/library/ee691831.aspx链接提供的信息.我已经更改了上面的调用,它在Office 2010 64位版本上运行良好.
Private Declare PtrSafe Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Run Code Online (Sandbox Code Playgroud)
问题是,我需要同样调用以处理旧的Office版本,并且它会在旧版本上引发编译错误.
有谁知道如何使这个电话适用于办公室2010和旧办公室版本.
我在Python中有一个JSON对象.我正在使用Python DB-API和SimpleJson.我试图将json插入MySQL表.
在我收到错误的那一刻,我相信这是由于JSON对象中的单引号''.
如何使用Python将我的JSON对象插入MySQL?
这是我收到的错误消息:
error: uncaptured python exception, closing channel
<twitstream.twitasync.TwitterStreamPOST connected at
0x7ff68f91d7e8> (<class '_mysql_exceptions.ProgrammingError'>:
(1064, "You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for
the right syntax to use near ''favorited': '0',
'in_reply_to_user_id': '52063869', 'contributors':
'NULL', 'tr' at line 1")
[/usr/lib/python2.5/asyncore.py|read|68]
[/usr/lib/python2.5/asyncore.py|handle_read_event|390]
[/usr/lib/python2.5/asynchat.py|handle_read|137]
[/usr/lib/python2.5/site-packages/twitstream-0.1-py2.5.egg/
twitstream/twitasync.py|found_terminator|55] [twitter.py|callback|26]
[build/bdist.linux-x86_64/egg/MySQLdb/cursors.py|execute|166]
[build/bdist.linux-x86_64/egg/MySQLdb/connections.py|defaulterrorhandler|35])
Run Code Online (Sandbox Code Playgroud)
另一个错误供参考
error: uncaptured python exception, closing channel
<twitstream.twitasync.TwitterStreamPOST connected at
0x7feb9d52b7e8> (<class '_mysql_exceptions.ProgrammingError'>:
(1064, "You have an error in …Run Code Online (Sandbox Code Playgroud) 我见过使用它的相似代码,但我在这一行给了我这个错误“无法解析 URLEncoder”:
String paramString = p.getName() + "=" + URLEncoder.encode(p.getValue(),"UTF-8");
它说函数是 encode(String s, String enc) 在“enc”上它说要使用的编码方案。
我正在运行 Eclipse SDK 版本:3.6.1,但我不知道如何解决此错误。
请看这个:
VIEWSTATE"id ="_ VIEWSTATE"value ="/ ........ (它大10倍但我删除了它)
这是HTML生成的HTML吗?ASP.NET MVC也做同样的事情吗?
我在Ubuntu 10.04上使用emacs 23.1.1.我希望使用2空格缩进在Python中编程.emacs看起来有python的默认模式(python.el?).
我把以下内容放在我的.emacs中:
;; Only spaces, no tabs
(setq indent-tabs-mode nil)
;; Always end a file with a newline
(setq require-final-newline nil)
;; Don't know which of these might work
(setq-default tab-width 2)
(setq-default python-indent 2)
(setq-default py-indent-offset 2)
Run Code Online (Sandbox Code Playgroud)
当我编辑Python文件时,它使用4空格缩进.当我尝试Ch v python-indent时,它说:
python-indent's value is 4
Local in buffer webpage_cache.py; global value is 2
This variable is safe as a file local variable if its value
satisfies the predicate `integerp'.
Documentation:
Number of columns for a unit of indentation …Run Code Online (Sandbox Code Playgroud) 标题几乎总结了它.既然我们List<T>有人为什么要用它ArrayList呢?我能想到的唯一原因是你是否被迫在List<T>实施之前使用旧版本的.NET ?
我昨天阅读了以下文章(http://blogs.sitepoint.com/2010/11/19/mysql-mistakes-php-developers/)并写了以下内容:
MySQL有许多数据库引擎,但你最有可能遇到MyISAM和InnoDB.
默认情况下使用MyISAM.但是,除非您创建一个非常简单或实验性的数据库,否则几乎肯定是错误的选择!MyISAM不支持对数据完整性至关重要的外键约束或事务.此外,无论何时插入或更新记录,整个表都会被锁定:随着使用量的增加,它会对性能产生不利影响.
解决方案很简单:使用InnoDB.
我一直使用MyISAM,因为它是默认的.你怎么看?
如果我要升级到InnoDB,在phpMyAdmin中,我可以编辑每个表并将其更改为innoDB,还是要执行更复杂的过程?
谢谢!