我有一个搜索屏幕,用户可以在其上指定名字,姓氏,学期或课程的任意组合.我不确定如何优化代码SQL Server 2005存储过程来处理这些可能的可选参数.什么是最有效的方式?每个组合的单独程序?将项目作为可空的parms并构建动态SQL?
我希望能够使用self初始化属性subviewGroup但是这给了我错误:属性'self.gridView'未在super.init调用时初始化
init(frame: NSRect) {
super.init(frame: frame)
subviewGroup = GridViewGroup(rows: 9, columns: 9, gridView: self)
}
/*** Properties ***/
let subviewGroup: GridViewGroup
Run Code Online (Sandbox Code Playgroud)
然后如果我在初始化后把super.init()放到这样的话
init(frame: NSRect) {
subviewGroup = GridViewGroup(rows: 9, columns: 9, gridView: self)
super.init(frame: frame)
}
/*** Properties ***/
let subviewGroup: GridViewGroup
Run Code Online (Sandbox Code Playgroud)
我收到错误:在super.init调用之前使用'self'
我知道我可以使用一个Optional,但是因为在init()调用之后它永远不会是nil似乎没必要.有没有办法正确地做到这一点,而不使subviewGroup成为可选项?
这是一个使用可选的工作示例
/*** Initializers ***/
init(frame: NSRect) {
super.init(frame: frame)
subviewGroup = GridViewGroup(rows: 9, columns: 9, gridView: self)
}
/*** Properties ***/
let subviewGroup: GridViewGroup?
Run Code Online (Sandbox Code Playgroud) 在使用Swift中的选项时,似乎有两种方法可以检查是否有可选类型nil.
var item: String? = "apple"
// Approach A
if item != nil {
"item is \(item!)"
} else {
"no item"
}
// Approach B
if let x = item {
"item is " + x
} else {
"no item"
}
Run Code Online (Sandbox Code Playgroud)
使用哪种方法检查可选项是否重要?
我想在这里做这个教程.我是初学者,这将是我的第一个iOS应用程序时期:
http://www.ioscreator.com/tutorials/calculator-tutorial-in-ios8-with-swift
这是我的代码:
导入UIKit
class ViewController: UIViewController {
var isTypingNumber = false
var firstNumber = 0
var secondNumber = 0
var operation = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBOutlet weak var calculatorDisplay: UILabel!
@IBAction func numberTapped(sender: AnyObject) {
var number = sender.currentTitle
if isTypingNumber {
calculatorDisplay.text = calculatorDisplay.text! + number!! …Run Code Online (Sandbox Code Playgroud) 我目前有一个测试应用程序来自学Core Location.这是一个带有按钮的应用程序,可以在按下按钮时显示您的位置,也可以在位置服务关闭时显示警报.
我得到一个致命的错误(在解包选项值时意外地发现nil)当位置服务打开并且我尝试打印字符串时.我在文档中找不到CLLocationManager.location.coordinate返回可选值的任何地方.我将包含代码段.谢谢!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
if CLLocationManager.authorizationStatus() == .NotDetermined {
locationManager.requestWhenInUseAuthorization()
}
}
@IBAction func testLocation(sender: AnyObject) {
switch CLLocationManager.authorizationStatus() {
case .AuthorizedAlways, .AuthorizedWhenInUse:
printCurrentCoordinates()
case .Restricted, .Denied:
disabledLocationSeriveAlert()
default:
println("Failed")
}
func printCurrentCoordinates() {
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
println("\(locationManager.location.coordinate.latitude)")
println("\(locationManager.location.coordinate.longitude)")
}
func disabledLocationSeriveAlert() {
let alert = UIAlertController(title: "Unable to retrieve location", message: "Enable location services in system settings", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil) …Run Code Online (Sandbox Code Playgroud) 是否java.util Optional.ofNullable工作正常使用的Mockito?
在代码执行期间,我遇到这样的事情:
User user = Optional.ofNullable(userProviderMock.findUser()).orElse(someMethod())
Run Code Online (Sandbox Code Playgroud)
我设置我的模拟行为如下:
when(userProviderMock.findUser()).thenReturn(new User());
Run Code Online (Sandbox Code Playgroud)
当我运行它时,userProviderMock返回new User()(调试确认),但不知何故someMethod()仍然执行.我真的不知道为什么会这样.有线索吗?
我如何使用Java Optional API以更优雅的方式重写以下代码:
first == null || second == null ? null : first + second;
Run Code Online (Sandbox Code Playgroud)
null如果两个变量中的任何一个是null或其他地方的总和,则代码应该返回.
我试图找到使用Java最简洁(和有意义)的方法,Optional从a读取第一个值Optional<String>并返回String(如果存在),或返回"NOT_FOUND".这是我正在使用的代码:
public static String getValue(Optional<String> input) {
return input.ifPresent(val -> val.get()).orElse("NOT_FOUND")
}
Run Code Online (Sandbox Code Playgroud)
Optional的方法显然有非常特殊的用途,但API让我感到困惑.
更新(4/13/2018):
我的问题中的代码不正确,因为如果我认为val是Optional中的值,那么val.get()没有任何意义.谢谢你指出这一点,@ okttman.
另外,我在接受的答案的注释中添加了我的问题的另一部分,即我需要一种方法来操作String值,如果存在,在返回之前.将orElse("NOT_FOUND")仍然适用,如果选不包含值.那么OptionalAPI 的可接受用途是什么来实现以下目标呢?
public static String getValue(Optional<String> input) {
return input.isPresent() ? input.get().substring(0,7).toUpperCase() : "NOT_FOUND";
}
Run Code Online (Sandbox Code Playgroud)
@Aominè的回答和后续评论都涉及这个问题的两个部分.
根据此,使用了比较运算符上的optional<T>和optional<U>应该工作条件是相同的运营商是为基础类型定义T和U.
我正在尝试以下示例,其中两个枚举在不同的命名空间中定义(这里是实时代码),并且无法弄清楚为什么它无法编译:
#include <optional>
namespace n1
{
enum class tag : unsigned {I,II,III};
}
namespace n2
{
enum class tag : unsigned {I,II,III};
}
bool operator<(const n1::tag& t1, const n2::tag& t2)
{
return static_cast<unsigned>(t1) < static_cast<unsigned>(t2);
}
int main()
{
const std::optional<n1::tag> o1(n1::tag::I);
const std::optional<n2::tag> o2(n2::tag::I);
bool t = (o1 < o2);
}
Run Code Online (Sandbox Code Playgroud)
我的GCC-8.2.0说:
invalid operands to binary expression ('const std::optional<n1::tag>' and 'const std::optional<n2::tag>')
有任何想法吗?我发现将每个枚举移出其命名空间,事情按预期工作(如此处).
我这里有一个很小的操场例子
fn main() {
let l = Some(3);
match &l {
None => {}
Some(_x) => {} // x is of type &i32
}
}
Run Code Online (Sandbox Code Playgroud)
我正在进行模式匹配&Option,如果Some(x)用作分支,为什么是xtype &i32?