我正在阅读Raywenderlich提供的教程,第29章测试的新功能,并遇到一个奇怪的问题.
以下是教程中将字符串转换为日期的代码:
// Convert date string to date.
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss zzz"
var date: NSDate? = dateFormatter.dateFromString(dateString)
Run Code Online (Sandbox Code Playgroud)
dateString是表单的
2014-06-21 14:56:00 EST
Run Code Online (Sandbox Code Playgroud)
但是,日期变量始终为零.
注意:在操场上播放此代码时,它可以正常工作,如图所示:

我在iOS SDK 8.0中工作.这是SDK的错误吗?
更新
我正在使用iOS 8的最新iPod Touch进行测试.
我有一个表单:remote => true表示它将通过ajax提交.
在控制器中,我有这样的代码:
before_filter: authenticate_user!, :only => [:create]
Run Code Online (Sandbox Code Playgroud)
因为我只允许确认的用户创建资源.
但是,当身份验证失败时,设计会提高一个
Completed 401 Unauthorized
Run Code Online (Sandbox Code Playgroud)
并且不会呈现不显眼的javascript.
但我希望事情是这样的:
设计在flash中设置一些消息,并渲染我的.js.erb,然后我向用户显示flash.
怎么实现呢?
我写了一个像这样的before_filter:
def authenticate_user
if request.xhr?
flash.now[:alert] = 'Error'
render :partial => "js_helpers/popover", :formats => :js
else
authenticate_user!
end
end
Run Code Online (Sandbox Code Playgroud)
但是,它没有按预期工作.日志文件显示:
Rendered js_helpers/_popover.js.erb (0.1ms)
Filter chain halted as :authenticate_user rendered or redirected
Run Code Online (Sandbox Code Playgroud)
js代码的功能是显示一个显示flash消息的弹出框.但看起来js代码根本不是很兴奋.
所以,我改变了方向:
def authenticate_user
if request.xhr?
flash.now[:alert] = 'Error'
render :js => "window.location = '/users/sign_up'"
else
authenticate_user!
end
end
Run Code Online (Sandbox Code Playgroud)
相反,我想重定向到注册页面.
但是,仍然存在问题.闪光似乎不起作用
我怎样才能实现我的目标:
当请求是ajax调用时,执行javascript代码或redirect_to新页面.在这两种情况下,我需要在flash中存储一些消息.
另外,javascript代码很长,所以我需要把它放在一个文件中.如何使用render:js在文件中执行javascript?
我想根据该行中的单元格调整UITableView的行高.
最初,我试图使用
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
但问题是,当表加载时,调用流似乎是:
第一个电话:
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
然后打电话:
(UIViewTableCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
这意味着,在生成单元格之前,我必须告诉表格行的高度.
但是,我想要的恰恰相反,即在生成单元格之后,我告诉表格这行的高度.
有没有办法实现这个目标?
我使用PostgreSQL作为作业队列.以下是我检索作业并更新其状态的查询:
UPDATE requests AS re
SET
started_at = NOW(),
finished_at = NULL
FROM (
SELECT
_re.*
FROM requests AS _re
WHERE
_re.state = 'pending'
AND
_re.started_at IS NULL
LIMIT 1
FOR UPDATE SKIP LOCKED
) AS sub
WHERE re.id = sub.id
RETURNING
sub.*
Run Code Online (Sandbox Code Playgroud)
现在,我有几台机器,在每台机器上我有一个带有多个线程的进程,并且在每个线程上我都有一个worker.同一进程中的所有工作程序共享一个连接池,通常具有10-20个连接.
问题是,上面的查询会多次返回一些行!
我找不到任何理由.有人可以帮忙吗?
更详细的说,我使用的是Python3和psycopg2.
更新:
我试过@ a_horse_with_no_name的答案,但似乎不行.
我注意到,一个请求被两个查询检索,started_at更新为:
2016-04-21 14:23:06.970897 + 08
和
2016-04-21 14:23:06.831345 + 08
它们仅相差0.14s.
我想知道当这两个连接执行内部SELECT子查询时,两个锁都没有建立?
更新:
更确切地说,我在一台机器上的1个过程中有200个工人(即200个线程).
我想尝试GPU编程.我的MacBook Air的GPU是Intel HD Graphics 3000,所以我想我不能使用CUDA.
我做了一些研究并遇到了OpenCL.但是在主页上,据说
OpenCL™(开放计算语言)是一种用于异构计算的低级API,可在基于CUDA的GPU上运行.
我认为我的GPU不是CUDA驱动的,所以也许OpenCL也不能使用.
然后我想知道如何在MacBook Air上进行GPU编程?
我在iOS 7中使用Text Kit来创建富文本编辑器.以下是问题所在.
首先,然后字体大小为16:

然后我使用代码插入图像:
// Image Attachment
NSTextAttachment *imageAttachment = [[NSTextAttachment alloc] init];
[imageAttachment setImage:image];
imageAttachment.bounds = CGRectMake(0, 0, width, height);
[self insertTextAttachment:imageAttachment];
Run Code Online (Sandbox Code Playgroud)
和
- (void)insertTextAttachment:(NSTextAttachment*)textAttachment {
NSMutableAttributedString *newContent = [[NSMutableAttributedString attributedStringWithAttachment:textAttachment] mutableCopy];
NSMutableAttributedString *currentContent = [self.contentTextView.attributedText mutableCopy];
[currentContent insertAttributedString:[[NSAttributedString alloc] initWithString:@"\n"] atIndex:mEditingRange.location];
[currentContent insertAttributedString:[[NSAttributedString alloc] initWithString:@"\n"] atIndex:mEditingRange.location];
mEditingRange.location++;
[currentContent replaceCharactersInRange:mEditingRange withAttributedString:newContent];
[currentContent setAttributes:@{NSFontAttributeName: [UIFont fontWithName:kOFontDefault size:16.0]} range:NSMakeRange(mEditingRange.location + newContent.length, 1)];
[self.contentTextView setAttributedText:currentContent];
}
Run Code Online (Sandbox Code Playgroud)
但是,插入图像后,文本字体意外更改:

我尝试使用以下代码来解决问题(在insertTextAttachment:方法中添加):
[currentContent setAttributes:@{NSFontAttributeName: [UIFont fontWithName:kOFontDefault size:16.0]} range:NSMakeRange(mEditingRange.location + newContent.length, 1)];
Run Code Online (Sandbox Code Playgroud)
问题部分修复,新行中的新文本使用正确的字体,但图像旁边的文本仍然是错误:

有人可以帮忙吗?
我试图重现视频C++ Weekly - Ep 48 - C++ 17的Variadic的结果using,但失败了.问题可以简化为以下代码段.
假设我有这样的通用结构:
template <class... T>
struct Container {
template <class... U>
Container(U... us) {}
};
Run Code Online (Sandbox Code Playgroud)
现在我可以Container用任何参数初始化a ,比如
auto d = Container(1,2,3);
Run Code Online (Sandbox Code Playgroud)
但是,编译器永远不会知道它是什么类型d.要解决这个问题,我们应该提供一个扣除指南,例如
template <class... U>
Container(U...) -> Container<double, int, bool>
Run Code Online (Sandbox Code Playgroud)
根据视频,编译器现在应该知道d有类型Container<double, int, bool>.
但是,代码无法按预期工作.打印时typeid(d).name(),无论我如何更改演绎指南中的返回类型,输出将始终被9ContainerIJEE转换为Container<>,表明此指南根本不指导编译器.
我正在使用gcc-7-snapshot-20170402,视频中的编译器是gcc-7-snapshot-20170130.
谁能告诉我这里有什么问题?
顺便说一句,如果我明确写
Container<bool, int> d = Container(1,2,3);
Container<char, char, char> d = Container(1,2,3);
...
Run Code Online (Sandbox Code Playgroud)
代码将始终编译,并提供像9containerIJbiEE …
我不确定如何在swift中声明委托.所以我检查了Apple的UITableView代码(通过命令+点击XCode中的UITableView),发现一些奇怪的东西.
以下是UITableView的委托的声明:

委托是一个无主(不安全)的可选值,但根据Apple的文档:

有人说
假定无主参考始终具有值
但是,代表当然可以是零,即没有价值.实际上,委托被声明为UITableViewDelegate?,这是一个可选值.不是有问题吗?
此外,以下是UITableViewDelegate的声明:

显然有几个可选功能.根据Apple的文档:

有人说
只有在协议标记为@objc属性时,才能指定可选的协议要求
但是,UITableViewDelegate未标记为@objc.不是有问题吗?
以下是我试图复制Apple定义协议和委托的方法的代码:
protocol CSImagePickerDelegate : NSObjectProtocol {
optional func imagerPickerDidPickImage(picker: CSImagePicker, image: UIImage)
}
class CSImagePicker: UIViewController {
unowned(unsafe) var delegate: CSImagePickerDelegate?
}
Run Code Online (Sandbox Code Playgroud)
不出所料,发生了两个错误:
所以我的问题是,为什么Apple可以做到这一点我不能?
顺便说一下,宣布委托的正确方法是什么?我想我应该用
弱var委托:SomeDelegate?
对?
我正在使用django-rest-framework.
有没有办法处理多个文件上传?似乎即使客户端发送多个文件(通过Web浏览器),MultiPartParser也只会选择第一个文件.