我正在尝试根据用户的用户名获取用户的值。我能够使用以下方法获得正确的用户:
class func getByUsername(username: String, callback: (records: [CKRecord]) -> ()){
getSummary(NSPredicate(format: "Username == %@", username)){(records: [CKRecord]) -> Void in
callback(records: records)
}
}
class func getSummary(predicate: NSPredicate, callback: (records: [CKRecord]) -> ()){
let query = CKQuery(recordType: "UserInfo", predicate: predicate)
let queryOperation = CKQueryOperation(query: query)
queryOperation.desiredKeys = ["ID"]
var records = [CKRecord]()
queryOperation.recordFetchedBlock = {record in records.append(record)}
queryOperation.queryCompletionBlock = {_ in callback(records: records)}
CKContainer.defaultContainer().publicCloudDatabase.addOperation(queryOperation)
}
Run Code Online (Sandbox Code Playgroud)
然后我可以通过调用以下命令来获取正确的记录:
getByUsername("Username"){(records: [CKRecord]) -> Void in
var record: CKRecord = records[0]
}
Run Code Online (Sandbox Code Playgroud)
我还可以通过使用获得正确的ID
var id: String …Run Code Online (Sandbox Code Playgroud) 我正在制作bukkit插件并返回一个函数List<Map<?, ?>>,如果我这样做:
List<Map<String, String>> circle = FileManager.area.getMapList("circles");
Run Code Online (Sandbox Code Playgroud)
我收到无法转换的错误.怎么办?
错误:
List<Map<?, ?>> cannot be converted into List<Map<String, String>>
Run Code Online (Sandbox Code Playgroud) 我正在尝试像摄像机视图一样做一个像摄像机视图,相机流填充所有屏幕.
这是代码:
func beginSession() {
var err : NSError? = nil
// try to open the device
let videoCapture = AVCaptureDeviceInput(device: captureDevice, error: &err)
if err != nil {
// Fail silently. Do better in the real world.
println("Capture session could not start: \(err?.description)")
return
}
// add video input
if captureSession.canAddInput(videoCapture) {
captureSession.addInput(videoCapture)
}
// config capture session
if !captureSession.running {
// set JPEG output
stillImageOutput = AVCaptureStillImageOutput()
let outputSettings = [ AVVideoCodecKey : AVVideoCodecJPEG ]
stillImageOutput!.outputSettings = outputSettings …Run Code Online (Sandbox Code Playgroud) 将来,C 风格的 for 语句将从 Swift 中删除。虽然有许多替代方法可以使用 C 风格的 for 语句,例如 usingstride或..<运算符,但这些仅适用于某些条件
例如,在旧版本的 swift 中,可以String.CharacterView.Index使用 C 样式的 for 语句循环遍历字符串的每个其他索引 ,
for var index = string.startIndex; index < string.endIndex; index = string.successor().successor(){
//code
}
Run Code Online (Sandbox Code Playgroud)
然而,这现在已被弃用。有一种方法可以做同样的事情,使用while循环
var index = string.startIndex
while index < string.endIndex{
//code
index = index.successor().successor()
}
Run Code Online (Sandbox Code Playgroud)
但这只不过是一种解决方法。有..<运算符,它非常适合循环遍历字符串的每个索引
for index in string.startIndex..<string.endIndex
Run Code Online (Sandbox Code Playgroud)
然而,这无助于循环遍历字符串的每个其他索引或字符串的每个第 n 个索引
除了循环之外,还有更多“快速”的方式来循环遍历字符串的所有其他索引while吗?这个问题不仅与字符串索引有关,而且只与具有.successor()一般功能的对象有关,例如wherestride和..<don't work。
我设法在Apples CloudKit中保存,更改和删除记录.我甚至收到订阅的通知,我不知道的是,我如何列出当前用户的所有订阅.
到目前为止,这是我的代码:
let operation = CKFetchSubscriptionsOperation()
operation.fetchSubscriptionCompletionBlock = { (d, e) -> Void in
println("got subscription")
if e != nil {
println("Error")
dump(e)
}
dump(d)
}
publicDatabase.addOperation(operation)
Run Code Online (Sandbox Code Playgroud)
我得到的是:
got subscription
Error
- <CKError 0x14db0ed0: "Invalid Arguments" (12)> #0
- 0 key/value pairs
Run Code Online (Sandbox Code Playgroud)
什么是无效参数?我如何获得所有已保存订阅的列表?
我有一个2D numpy数组,我需要根据条件进行掩码,以便我可以对蒙版数组应用一个操作,然后将屏蔽的值恢复为原始值.
例如:
import numpy as np
array = np.random.random((3,3))
condition = np.random.randint(0, 2, (3,3))
masked = np.ma.array(array, mask=condition)
masked += 2.0
Run Code Online (Sandbox Code Playgroud)
但是,如何将屏蔽值更改回原始值并在将给定操作应用于屏蔽数组后"移除"屏蔽?
我需要这样做的原因是我基于一组条件生成一个布尔数组,我需要修改满足条件的数组元素.
我可以使用布尔索引来实现1D数组,但是对于2D数组我需要保留其原始形状,即.不返回仅具有满足条件的值的1D数组.
我有一个应用程序,我目前正在使用SwiftKeychainWrapper.下面是代码我有检查,如果retrievedString是nil.但是我还在进入retrievedString: nil控制台.
不应该运行if-let语句中的代码,或者我是否正在使用/理解if-let错误?
在给定的示例中,使用if-let打开可选值的正确方法是什么?
if let retrievedString: String? = KeychainWrapper.stringForKey("username") {
print("retrievedString: \(retrievedString)")
//value not nil
} else {
//Value is nil
}
Run Code Online (Sandbox Code Playgroud) 无法在[DEVICE NAME]上安装XXXXXX.app XXXXX.app需要[DEVICE NAME]不支持的""功能
尝试在设备中安装时出现此错误,是否有人遇到过同样的情况?
我正在使用Xcode 7.2
我试图SecItemCopyMatching在我的钥匙串实用程序类中调用,以便从钥匙串中获取数据,但我遇到了获取result参数的问题,UnsafeMutablePointer<CFTypeRef?>.
最初的声明(在Swift 2中,在迁移到Swift 3之前)是
// query is a dictionary of [String : AnyObject]
var result: Data?
let status = withUnsafeMutablePointer(to: &result) {
SecItemCopyMatching(query as CFDictionary, UnsafeMutablePointer($0))
}
Run Code Online (Sandbox Code Playgroud)
但是在Swift 3中,您现在需要调用.withMemoryRebound才能查看内存.基于Xcode告诉你的事情,我尝试了这个
var result: Data?
let status = withUnsafeMutablePointer(to: &result){
$0.withMemoryRebound(to: Data.self, capacity: 1){
SecItemCopyMatching(query as CFDictionary, UnsafePointer($0))
}
}
Run Code Online (Sandbox Code Playgroud)
然而,这样做,我得到一个错误
无法将类型'
UnsafePointer<_>'的值转换为预期的参数类型'UnsafeMutablePointer<CFTypeRef?>?'
所以,我尝试使用CFTypeRef而不是Data
var result: CFTypeRef?
let status = withUnsafeMutablePointer(to: &result){
$0.withMemoryRebound(to: CFTypeRef.self, capacity: 1){
SecItemCopyMatching(query as CFDictionary, …Run Code Online (Sandbox Code Playgroud) unsafe-pointers core-foundation swift swift3 unsafemutablepointer
我有一个类似的列表:
xxxxx|xxxxxx|xxxxx@example.com
xxxxx|xxxxxx|xxxxx@example.com
xxxxx|xxxxxx|xxxxx@example.com
Run Code Online (Sandbox Code Playgroud)
x可以是字母和数字,但没有别的.
我想要做的是替换第二个右边的所有内容(包括角色)|.结果应该如下:
xxxxx|xxxxx
Run Code Online (Sandbox Code Playgroud)
我想用正则表达式做到这一点,以便我可以替换Notepad ++中的所有事件.