我正在从事的项目旨在展示一个.gif
带有背景的可移动物体。目前,如果我使用的super.paint(g);
话,角色和背景画就会闪烁。如果我不这样做,那么它会留下像所包含的图像一样的痕迹。我不知道为什么会发生这种情况,需要帮助。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JLabel;
public class keyboardinput extends JFrame implements ActionListener, KeyListener {
private JPanel contentPane;
private Image playerModelRight;
private Image playerModelLeft;
private Image playerModelAttackRight;
private Image playerModelAttackLeft;
private Image playerModelStandRight;
private Image playerModelStandLeft;
private Image background;
private Image doubleBuffer;
private int direction;
private Timer …
Run Code Online (Sandbox Code Playgroud) 确保NSString仅包含字母az和AZ的最佳方法是什么.
我已经尝试了以下代码,但由于某些原因它不起作用:
NSString *myegex = @"[A-Za-z]";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", myregex];
if (![emailTest evaluateWithObject:self.initials.text]) {
// print error
return;
}
Run Code Online (Sandbox Code Playgroud) 我刚开始用斯坦福教程学习Swift.我有Xcode 6.3.2.我在操作中遇到了一个错误,switch
无法理解如何解决它.
我也附了一个截图
@IBAction func operate(sender: UIButton) {
let operation = sender.currentTitle!
if userIsInTheMiddleOfTypingANumber{
enter()
}
switch operation{
case "?": performOperation {$0+$1}
case "??": performOperation {$0*$1}
case "?": performOperation {$1/$0}
case "?": performOperation {$1-$0}
default:break
}
}
private func performOperation(operation : (Double,Double)->Double){
if(operandStack.count>=2){
DisplayValue = operation(operandStack.removeLast(),operandStack.removeLast())
enter()
}
}
Run Code Online (Sandbox Code Playgroud)
我从服务获取一些JSON数据,一个属性是表示图像URL的字符串,似乎返回的是NULL,我做检查但是XCode在我的if语句之前中断并生成异常.这是我的代码如下:
- (void)configureCellForAlbum:(Album *)album {
self.albumTitle.text = album.albumTitle;
self.artisteName.text = album.artisteName;
NSURL *imageUrl = [NSURL URLWithString:album.thumbnail];
if (imageUrl == nil) {
self.albumThumbnail.image = [UIImage imageNamed:@"music_record"];
}
else {
self.albumThumbnail.imageURL = imageUrl;
}
}
Run Code Online (Sandbox Code Playgroud)
例外是
由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [NSNull length]:发送到实例的无法识别的选择器.
我如何进行检查,以便如果值为null,则使用本地图像但如果不为null则使用返回的图像字符串url?
我viewDidLoad
在UIViewController
子类中有以下方法实现:
var scrollView = UIScrollView.newAutoLayoutView()
var contentView = UIView.newAutoLayoutView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(scrollView)
scrollView.autoPinEdgesToSuperviewEdgesWithInsets(UIEdgeInsetsZero)
scrollView.addSubview(contentView)
contentView.autoPinEdgesToSuperviewEdgesWithInsets(UIEdgeInsetsZero)
contentView.autoMatchDimension(.Height, toDimension: .Height, ofView: view)
contentView.autoMatchDimension(.Width, toDimension: .Width, ofView: view)
contentView.addSubview(customView)
customView.autoPinEdgeToSuperviewEdge(.Top, withInset:0)
customView.autoPinEdgeToSuperviewEdge(.Left, withInset:15)
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行应用程序时,内容不会滚动.我发现很少有PureLayout文档和示例,并且没有关于滚动视图的清楚.有人可以帮我这个吗?
Swift在Xcode 6.3中发生了很大的变化.我不得不在我的每个应用程序中替换几十个地方as
- > as!
.为什么,现在的规则是什么?
我试图通过简单的快速代码使用iOS模拟器获得位置.但是在调试会话中,不会调用回调函数.我将虚拟位置设置为iOS模拟器.
// ViewController.swift
// helloWorld
//
// Created by on 5/17/15.
// Copyright (c) 2015 All rights reserved.
//
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var locMan: CLLocationManager!
var lat: CLLocationDegrees!
var lng: CLLocationDegrees!
var hasPosData: Bool
required init(coder aDecoder: NSCoder) {
locMan = CLLocationManager()
lat = CLLocationDegrees()
lng = CLLocationDegrees()
hasPosData = false
super.init(coder: aDecoder)
}
func retreiveCurrentPos(){
locMan.delegate = self
locMan.requestAlwaysAuthorization()
// start using GPS function
locMan.startUpdatingLocation()
}
func saveResult2File(){
var contents: String
let dirToSave …
Run Code Online (Sandbox Code Playgroud) 我试图理解这种转换,但我只是不明白。
RVConfig *config = [RVConfig defaultConfig];
Rover *rover = [Rover setup:config];
[rover startMonitoring];
Run Code Online (Sandbox Code Playgroud)
这是我所拥有的
RVConfig.defaultConfig()
Rover.setup(config: Rover(rover)
Rover.startMonitoring(Rover)
Run Code Online (Sandbox Code Playgroud) 我想实现一个for循环,这样对于每次迭代,它将等待一秒钟才能进入下一个循环.
for (NSUInteger i = 0; i <=3; i++) {
//...do something
//...wait one second
}
Run Code Online (Sandbox Code Playgroud) 假设我在一个类中定义变量ViewController
,并且想要从另一个类访问该变量,比如说BLEHandler
.我该怎么做呢?我遇到了麻烦NSUserDefaults
,所以除此之外的方法将不胜感激.
我找到了一个用于连接Microsoft SQL Server数据库的开源Objective-C库.
问题是,我想同步使用它.
这就是我的Swift项目使用库的方式.
func execute(query: String) {
self.client.connect(host + ":" + port, username: username, password: password, database: database) { (connected) -> Void in
if connected {
self.client.execute(query, completion: { (results: Array<AnyObject>!) -> Void in
self.result = results[0] as! Array<AnyObject>
})
}
}
}
Run Code Online (Sandbox Code Playgroud)
传递的块由库异步执行.有没有办法让代码同步执行,这样每当我调用时execute
,该线程在execute
返回之前等待库工作完成?
众所周知,JavaScript"将"变量"提升"到文件或范围的顶部.但根据我的理解,使用let
是相同的,var
只是let
局限于它所定义的范围.
作为一种编译语言而不是解释,我们可以假设Swift不这样做吗?
例如:
x = 10
var y = x + 10
var x
Run Code Online (Sandbox Code Playgroud) swift ×8
ios ×5
objective-c ×3
asynchronous ×1
autolayout ×1
casting ×1
hoisting ×1
iteration ×1
java ×1
nsstring ×1
pure-layout ×1
regex ×1
sql ×1
swing ×1
uiscrollview ×1