小编Mik*_*e A的帖子

编码问题:Cocoa Error 261?

所以我使用以下方法从我的iPhone应用程序中的PHP脚本中获取JSON字符串:

NSURL *baseURL = [NSURL URLWithString:@"test.php"];
NSError *encodeError = [[NSError alloc] init];
NSString *jsonString = [NSString stringWithContentsOfURL:baseURL encoding:NSUTF8StringEncoding error:&encodeError];
NSLog(@"Error: %@", [encodeError localizedDescription]);
NSLog(@"STRING: %@", jsonString);
Run Code Online (Sandbox Code Playgroud)

JSON字符串在测试输出时验证.现在我遇到了编码问题.当我获取单个echo'd行时,例如:

{ "testKey":"é" }
Run Code Online (Sandbox Code Playgroud)

JSON解析器工作正常,我能够创建一个有效的JSON对象.但是,当我获取2MB JSON字符串时,我会看到:

Error: Operation could not be completed. (Cocoa error 261.)
Run Code Online (Sandbox Code Playgroud)

和一个空字符串.我的PHP文件本身是UTF8,我没有使用utf8_encode(),因为这似乎是对数据进行双重编码,因为我已经将数据拉为NSUTF8StringEncoding.无论哪种方式,在我的单回显测试中,这是允许我在构建JSON对象时成功记录\ ASDAS样式UTF8转义的方法.

在较大字符串的情况下可能导致错误的原因是什么?

此外,我不确定它是否有所作为,但我在我解析的php数据上使用php函数addslashes()来计算引号等构建JSON字符串时.

iphone cocoa json objective-c

20
推荐指数
3
解决办法
1万
查看次数

铁轨项目中的耙子范围?

在我正在研究的项目中,我有很多使用rakes运行的解析器.当使用已存在于另一个rake中的方法名称,并且因为它们都使用相同的环境时,我会遇到冲突.

有没有办法限制其命名空间中的rake文件的范围?我认为那是命名空间的全部意义吗?

例:

namespace :test do
  task :test_task => :environment do
      ...
  end

  def test_method(argument)
    ...
  end    
end

namespace :other_test do
  task :test_task => :environment do
    ...
  end

  def test_method(argument, argument2)
    ...
  end
end
Run Code Online (Sandbox Code Playgroud)

在这种情况下,运行时rake test:test_task我会收到无效数量的参数错误.另一方面,如果我在任务本身内定义方法,我必须按顺序将方法保留在rake文件的顶部.这有点令人困惑和丑陋.

那只是一个必要的邪恶吗?

谢谢!

rake ruby-on-rails

7
推荐指数
1
解决办法
2003
查看次数

动画CAShapeLayer Pie

我正在尝试使用创建一个简单的动画饼图CAShapeLayer.我想让它从0动画到提供的百分比.

要创建形状图层,我使用:

CGMutablePathRef piePath = CGPathCreateMutable();
CGPathMoveToPoint(piePath, NULL, self.frame.size.width/2, self.frame.size.height/2);
CGPathAddLineToPoint(piePath, NULL, self.frame.size.width/2, 0);
CGPathAddArc(piePath, NULL, self.frame.size.width/2, self.frame.size.height/2, radius, DEGREES_TO_RADIANS(-90), DEGREES_TO_RADIANS(-90), 0);        
CGPathAddLineToPoint(piePath, NULL, self.frame.size.width/2 + radius * cos(DEGREES_TO_RADIANS(-90)), self.frame.size.height/2 + radius * sin(DEGREES_TO_RADIANS(-90)));

pie = [CAShapeLayer layer];
pie.fillColor = [UIColor redColor].CGColor;
pie.path = piePath;

[self.layer addSublayer:pie];
Run Code Online (Sandbox Code Playgroud)

然后动画我用:

CGMutablePathRef newPiePath = CGPathCreateMutable();
CGPathAddLineToPoint(newPiePath, NULL, self.frame.size.width/2, 0);    
CGPathMoveToPoint(newPiePath, NULL, self.frame.size.width/2, self.frame.size.height/2);
CGPathAddArc(newPiePath, NULL, self.frame.size.width/2, self.frame.size.height/2, radius, DEGREES_TO_RADIANS(-90), DEGREES_TO_RADIANS(125), 0);                
CGPathAddLineToPoint(newPiePath, NULL, self.frame.size.width/2 + radius * cos(DEGREES_TO_RADIANS(125)), self.frame.size.height/2 + radius …
Run Code Online (Sandbox Code Playgroud)

iphone core-animation core-graphics ipad ios

7
推荐指数
1
解决办法
1万
查看次数

arrayWithContentsOfURL的正确用法是什么?

这有点基本; 我正在尝试检索iPhone应用程序的http数据.

我有www.myhost.com/test.html

<array><string>test</string><string>test2</string></array>
Run Code Online (Sandbox Code Playgroud)

然后我有

NSURL *baseURL = [NSURLRLWithString:@"http://www.myhost.com/test.html"];
NSArray *array = [NSArray arrayWithContentsOfURL:baseURL];
NSLog(@"%@", [array description]);
Run Code Online (Sandbox Code Playgroud)

哪个继续返回(null).我错过了什么?谢谢!

iphone objective-c

4
推荐指数
2
解决办法
2496
查看次数

Mongoid和查询嵌入式位置?

我有一个模型:

class City
    include Mongoid::Document
    field :name  
    embeds_many :stores

    index [["stores.location", Mongoid::GEO2D]]
end

class Store
    include Mongoid::Document
    field :name
    field :location, :type => Array
    embedded_in :cities, :inverse_of => :stores
end
Run Code Online (Sandbox Code Playgroud)

然后我试着打电话City.stores.near(@location).

我想查询City集合以返回Store在附近位置至少有1个的所有城市.我该如何设置索引?什么是最快的电话?

我使用Mongoid文档阅读,index [[:location, Mongo::GEO2D]]但我不确定这是如何应用于嵌入式文档,或者如何只获取City而不是所有Stop文档.

ruby ruby-on-rails mongodb mongoid

3
推荐指数
1
解决办法
938
查看次数

在一个声明中使用ActiveRecord序列化多个属性?

我有一个模型,有大约10个属性,我希望JSON在我的数据库中序列化.

有办法吗?

serialize attribute1, JSON
// ...
serialize attribute10, JSON
Run Code Online (Sandbox Code Playgroud)

一行?因为serialize [attribute1, ..., attribute10], JSON不起作用.

谢谢!

ruby activerecord ruby-on-rails

1
推荐指数
1
解决办法
2651
查看次数

在Capistrano部署期间如何访问环境变量?

我的Rails(4.2)应用程序通过Ubuntu(14.02)系统上的Passenger(5.0.28)+ Apache(2.4.7)运行,使用rbenv管理的ruby(2.3.0)。我使用Capistrano(3.4.0)进行部署。

我所有的环境变量都在一个非常简单的profile.d脚本中设置。

#!/bin/sh
export VAR1=VAL1
export VAR2=VAL2
Run Code Online (Sandbox Code Playgroud)

这就像一个魅力。我的应用程序ENV具有所有正确的变量,Secrets.yml并已正确填充...除了通过ssh与Capistrano进行部署外,其他所有功能均正常运行。

在我的deploy.rb书中,我认为以下内容是相对的:

set :ssh_options, {
forward_agent: true,
paranoid: true,
keys: "~/.ssh/id_rsa.pub"
}
Run Code Online (Sandbox Code Playgroud)

Capistrano文档非常有限,并且ssh \ server配置不是我的强项,我似乎无法弄清楚为什么ENVCapistrano无法看到我的变量。如果我puts ENV.inspect在部署流程中运行,则会得到诸如"TERM_PROGRAM"=>"Apple_Terminal"本地机器用户信息之类的信息。Capistrano为什么不使用远程环境?如何在服务器端或在部署脚本中修改配置以解决此问题?

谢谢您的帮助。

capistrano ruby-on-rails passenger rbenv

1
推荐指数
1
解决办法
1737
查看次数