在ac#initialiser中,如果条件为false,我想不设置属性.
像这样的东西:
ServerConnection serverConnection = new ServerConnection()
{
ServerInstance = server,
LoginSecure = windowsAuthentication,
if (!windowsAuthentication)
{
Login = user,
Password = password
}
};
Run Code Online (Sandbox Code Playgroud)
可以办到?怎么样?
我正在尝试将我的应用程序部署到Heroku,我之前在我的Windows机器上完成了这个,现在我正在使用mac.
我第一次尝试使用Postgresql.
我的Gemfile中有以下内容:
gem 'pg'
Run Code Online (Sandbox Code Playgroud)
编辑:
AndrewDavis-OSX:lunchbox ardavis$ rvm list
rvm rubies
=> ruby-1.9.2-p180 [ x86_64 ]
AndrewDavis-OSX:lunchbox ardavis$ heroku rake db:migrate
rake aborted!
/app/config/initializers/session_store.rb:3: syntax error, unexpected ':', expecting $end
App::Application.config.session_store :cookie_store, key: '_app_session'
^
(See full trace by running task with --trace)
(in /app)
Run Code Online (Sandbox Code Playgroud)
如您所见,我正在运行ruby 1.9.2.我的heroku迁移有错误.
编辑2:
刚刚使用Rails 3.1.rc1创建了一个全新的rails应用程序.我将gemfile设置为include
组:制作做宝石'therubyracer-heroku','0.8.1.pre3'gemite'pg'结束
我做了一个快速的git init,提交,然后'heroku create'和'git push heroku master'.一切都很好.然而问题是当我尝试'heroku rake db:migrate'时.我得到了你上面看到的同样的错误.
TEMP FIX编辑:
所以...如果我更改我的config/initializers/session_store.rb
App::Application.config.session_store :cookie_store, key: '_app_session'
Run Code Online (Sandbox Code Playgroud)
至
App::Application.config.session_store :cookie_store, :key => '_app_session'
Run Code Online (Sandbox Code Playgroud)
并更改我的config/initializers/wrap_parameters.rb
ActionController::Base.wrap_parameters format: [:json]
Run Code Online (Sandbox Code Playgroud)
至
ActionController::Base.wrap_parameters :format …Run Code Online (Sandbox Code Playgroud) deployment ruby-on-rails heroku initializer ruby-on-rails-3.1
我正在使用预测宝石.我将推荐人初始化为initializers/predictor.rb:
require 'course_recommender'
recommender = CourseRecommender.new
# Add records to the recommender.
recommender.add_to_matrix!(:topics, "topic-1", "course-1")
recommender.add_to_matrix!(:topics, "topic-2", "course-1")
recommender.add_to_matrix!(:topics, "topic-1", "course-2")
Run Code Online (Sandbox Code Playgroud)
然后我想在CourseController中使用推荐器,如下所示:
class CourseController < ApplicationController
def show
# I would like to access the recommender here.
similiar_courses = recommender.similarities_for("course-1")
end
end
Run Code Online (Sandbox Code Playgroud)
我怎样才能设置recommender为应用程序控制器变量,以便在控制器中访问它?
在rails中,在创建初始化程序时,是否像向config/initializer director添加文件一样简单,还是需要在其他地方进行更改?
我问,因为每次为gem创建初始化程序时我都会遇到错误...而且不确定是不是因为我错过了创建初始化程序的步骤.
来自另一个问题:
由于C++ 17,auto x0{1, 2, 3, 4};以前推断出初始化列表,不再允许(当然,我们可以使用auto x0 = {1, 2, 3, 4};...).现在一如既往地避免统一初始化(例如std::vector<int> v({1, 2, 3, 4});,使用初始化列表作为参数进行显式构造函数调用),并且类似于定义良好auto x(7);(我不会自己使用的构造......),我想出了以下内容:
auto x({1, 2, 3, 4});
// -> std::initializer_list<int> x({1, 2, 3, 4});
Run Code Online (Sandbox Code Playgroud)
这是用GCC 7.2.0(mingw64)编译的,但是发出警告(而评论版再次没有):
list-initializer for non-class type must not be parenthesized
我在标准中找不到任何相关内容,所以现在的问题是(出于纯粹的兴趣......):
为什么不允许这样做?(这是否被标准所涵盖,或者我们是否需要将此视为GCC错误?)
以下代码编译,但失败并带有NullReferenceException:
class Test
{
public Dictionary<string, string> Dictionary { get; set; }
}
static void Main(string[] args)
{
var x = new Test
{
Dictionary = // fails
{
{ "key", "value" }, { "key2", "value2" }
}
};
}
Run Code Online (Sandbox Code Playgroud)
如果用以下内容替换标记为"失败"的行,则可以正常工作(如预期的那样):
Dictionary = new Dictionary<string, string>
Run Code Online (Sandbox Code Playgroud)
失败的语法是否有任何目的 - 是否可以在其他情况下成功使用?或者这是编译器的疏忽?
我正在攻读关于Java的考试.在我学习的时候,我遇到了java中我不熟悉的语法.例如花括号({})取消了没有名称的类主体,有些具有静态关键字.我发现它们被称为"初始化器".任何人都可以帮我指出它们之间的主要差异以及它们与构造函数的区别.谢谢
我正在尝试创建一个简单的UIBarButtonItem的Swift子类:
class LabelBarButtonItem: UIBarButtonItem {
let label: UILabel
init(text: String) {
self.label = UILabel(frame: CGRectZero)
self.label.tintColor = UIColor.grayColor()
super.init(customView: self.label)
self.label.text = text
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试实例化这个:
let countButton = LabelBarButtonItem(text: "0 items")
Run Code Online (Sandbox Code Playgroud)
代码编译正确但在运行时失败:
fatal error: use of unimplemented initializer 'init()' for class 'TestProject.LabelBarButtonItem'
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么在这种情况下会发生这种情况,或者一般来说是什么原则导致这个问题.在init(text)初始化应直接委托给init(customView)超类.为什么要init()打电话?它不应该涉及.
任何人都可以解释发生了什么吗?当我试图继承其他UIKit类时,会出现类似的问题
我正在MKAnnotationView我的项目中创建一个子类.它需要有两个属性来存储子视图,我需要在开头的某个地方进行初始化.
MKAnnotationView在其文档中列出了一个初始化程序initWithAnnotation:reuseIdentifier:,所以我想我只是覆盖它:
class PulsatingDotMarker: MKAnnotationView {
let innerCircle: UIView
let outerCircle: UIView
override init!(annotation: MKAnnotation!, reuseIdentifier: String!) {
innerCircle = ...
outerCircle = ...
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
...
}
Run Code Online (Sandbox Code Playgroud)
但这会导致运行时异常:
致命错误:对'PulsatingDotMarker'类使用未实现的初始化程序'init(frame :)'
好的,所以我猜initWithAnnotation:reuseIdentifier:内部调用initWithFrame:,所以可能是我应该覆盖的那个.我们试试看:
class PulsatingDotMarker: MKAnnotationView {
let innerCircle: UIView
let outerCircle: UIView
override init(frame: CGRect) {
innerCircle = ...
outerCircle = ...
super.init(frame: frame)
}
...
}
Run Code Online (Sandbox Code Playgroud)
但是,这会在创建注释视图对象时导致编译错误:
调用中的额外参数'reuseIdentifier'
嗯,所以如果我实现(必需的)初始化器initWithFrame:,它现在会丢失默认的初始化器initWithAnnotation:reuseIdentifier:?
也许如果我添加一个覆盖initWithAnnotation:reuseIdentifier:,只是调用super …
我正在尝试在Perl6中编写一些逻辑语句.
我做了逻辑运算符:
multi sub prefix:<¬> ($n) {
return not $n;
}
multi sub infix:<?> ($n, $b) {
return ($n and $b);
}
multi sub infix:<?> ($n, $b) {
return ($n or $b);
}
multi sub infix:<?> ($n, $b) {
if $n == True and $b == True {
return True;
} elsif $n == True and $b == False {
return False;
} elsif $n == False {
return True;
}
}
multi sub infix:<?> ($n, $b) {
return $b …Run Code Online (Sandbox Code Playgroud) initializer ×10
c# ×2
swift ×2
c++ ×1
c++17 ×1
constants ×1
constructor ×1
controller ×1
declaration ×1
deployment ×1
dictionary ×1
heroku ×1
java ×1
mapkit ×1
objective-c ×1
perl6 ×1
raku ×1
static ×1
syntax ×1