我正在使用 CoreBluetooth 框架制作一个应用程序,最近我将它移植到了 swift 2.0。在我的代码中,我有一个 CBCharacteristic 的全局变量,在 swift 1.2 中我可以这样做:
var bob:CBCharacteristic = CBCharacteristic()
Run Code Online (Sandbox Code Playgroud)
但是现在它给我的错误是 CBCharacteristic 的 init() 函数已被显式隐藏,因此我无法对其进行初始化。有没有办法解决这个问题?
我目前正在使用 Xcode 7.5 betas 和 Swift 2 进行开发。我选择让我的项目在所有 iPhone 上运行。但是,我不想在 iPhone 4s 上运行它,因为屏幕尺寸太小。无论如何,我可以让它仅在 iPhone 5 - 6s 上运行吗?
我正在尝试使用按位非运算符在 Swift 中翻转数字的所有位 ~
func binary(int: Int) -> String {
return String(int, radix: 2)
}
let num = 0b11110000
binary(num) //prints "11110000"
let notNum = ~num
binary(notNum) //prints "-11110001"
Run Code Online (Sandbox Code Playgroud)
我的理解是notNum应该打印出00001111( docs ) 而不是打印出来-11110001。这里发生了什么?
我想在两点之间的圆弧上显示图像。我有一个起始 CGPoint 和一个结束 CGPoint。我已经使用 SceneKit 和其他生成整个拱门和图形的答案看到了类似的答案。我只是在常规视图中工作。
我的最终目标是得到这样的结果(字母代表图像):
我需要知道在哪里设置 B & C 的中心。
let firstLabel = UILabel(frame: CGRectMake(12, 200, 50, 50))
let lastLabel = UILabel(frame: CGRectMake(250, 360, 50, 50))
Run Code Online (Sandbox Code Playgroud) 尝试构建我的数据时,我不断收到 Use of unresolved identifier 'CLLocationCoordinate2D' 。这是一个保存数据的类,它附加了一个我稍后将在 Map 和 TableViewController 中使用的数组。关于为什么我收到此错误的任何想法?
导入基础导入 UIKit 导入 CoreData
class StationsData: NSObject {
//define the varibles
init () {
}
var stations: [StationItem]
//initiate the array and assig the values to the array
required init(coder aDecoder: NSCoder) {
stations = [StationItem]()
let row0 = StationItem(title: "Station1", address: "25 Israel Eldad St, Jerusalem", coordinate: CLLocationCoordinate2D(latitude: 31.7513239, longitude: 35.224822899999936))
stations.append(row0)
let row1 = StationItem(title: "Station2", address: "25 Israel Eldad St, Jerusalem", coordinate: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)) …Run Code Online (Sandbox Code Playgroud) 我已经阅读过类似的问题/答案,但没有一个能解决我的问题。
我有一个这样的对象
class Chat {
unreadMessages: Int = 0
messages: Int = 0
}
Run Code Online (Sandbox Code Playgroud)
然后我有一组 Chat 对象,我需要按多个条件对其进行排序,首先通过与 grater 未读消息聊天,然后与 grater total 消息聊天。
埃斯。
Obj TotMes Unread
Chat A 10 3
Chat B 1 0
Chat C 4 0
Chat D 9 9
Run Code Online (Sandbox Code Playgroud)
所需的输出:
Chat D 9 9
Chat A 10 3
Chat C 4 0
Chat B 1 0
Run Code Online (Sandbox Code Playgroud)
我尝试使用这种 alhoritm,但似乎不起作用:
let sorted = chats.sort({ (c1, c2) -> Bool in
if c1.unreadMessages > c2.unreadMessages {
return true
}
if c1.messages …Run Code Online (Sandbox Code Playgroud) 我正在为 Apple TV 制作 tvOS 应用程序,但我的UITableView. 当我点击一个UITableViewCell什么都没有发生。我正在使用,targetForAction但它似乎不起作用。
这是我的代码:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
}
let allData = ["Los Angeles","New York","San Fransisco"]
@IBOutlet weak var tableView: UITableView!
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .Default, reuseIdentifier: nil)
cell.textLabel?.text = "\(allData[indexPath.row])"
cell.targetForAction("buttonClicked:", withSender: …Run Code Online (Sandbox Code Playgroud) 从udacity课程我尝试下面的代码
let password = "Meet me in St. Louis"
let newPassword = password.replacingOccurrences(of: "e", with: "3")
Run Code Online (Sandbox Code Playgroud)
这是给予
Playground execution failed: Introduction.xcplaygroundpage:31:19: error: value of type 'String' has no member 'replacingOccurrences'
let newPassword = password.replacingOccurrences(of: "e", with: "3")
^~~~~~~~ ~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
我必须使用
let password = "Meet me in St. Louis"
let newPassword = password.stringByReplacingOccurrencesOfString("me", withString: "ss")
Run Code Online (Sandbox Code Playgroud)
任何线索为什么会出现这个错误?我使用的是swift 2.2版
我已经尝试了很多带完成按钮的数字键盘,但在SWIFT 2.0中找不到正确的答案.
我使用CAShapeLayer在屏幕上画一条线.在touchesEnded方法中,我想检查"线是否通过该点?".在我的代码中,当我按下屏幕的任何部分时,该方法包含返回始终为true.也许,我在line.frame =(view?.bounds)中遇到了问题!.我该如何解决?对不起,我的英语不好.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let firstPosition = touch?.location(in: self)
if atPoint(firstPosition!) == lvl1 {
let firstPositionX = firstPosition?.x
let firstPositionY = frame.size.height - (firstPosition?.y)!
view?.layer.addSublayer(line)
line.lineWidth = 8
let color = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1).cgColor
line.strokeColor = color
line.fillColor = nil
line.frame = (view?.bounds)!
path.move(to: CGPoint(x: firstPositionX!, y: firstPositionY))
}
}
override func touchesMoved(_ touches: …Run Code Online (Sandbox Code Playgroud)