由于我有 JS 背景,这就是我通过存储在变量中的名称调用函数的方式:
var obj = {
foobar: function(param) {
// do something
}
};
var key = "foobar";
obj[key](123);
Run Code Online (Sandbox Code Playgroud)
现在我想在 Swift 中重新创建它,例如:
struct obj = {
func foobar(param) {
// do something
}
}
let key:String = "foobar"
obj[key](123)
Run Code Online (Sandbox Code Playgroud)
不幸的是上面的代码给出了Type 'obj.Type' has no subscript members
有没有办法通过结构、类或字典中的名称调用函数(如果甚至可以在字典中存储函数?)
编辑-更多背景:
我有一系列用户提供的内容:
let arr = ["apples", "oranges", "pears"]
Run Code Online (Sandbox Code Playgroud)
但这个数组最多可以有 20 项。根据数组的每一项,我需要执行某些操作。所以我迭代数组:
for (key, _) in arr {
if (key == "apples") {
handleApples()
}
if (key == "oranges") {
handleOranges()
}
// …Run Code Online (Sandbox Code Playgroud) 嘿,我正在尝试在我的设备上运行实时提要。现在我想每 3 秒拍一张照片,但每次都拍。它发出快门声。这是糟糕的用户体验。
因此,我想从前置摄像头运行实时摄像头流并在特定持续时间(~3 秒)捕获帧。
如何从实时摄像机源中提取帧并将其存储在 UIImage 变量中?
谢谢和干杯!
你如何在 Swift 3 中创建一个 UIToolbar?我想要左边的动作和右边的动作以及中间的标题,就像大多数应用程序一样。它看起来也像一个没有后退箭头或堆栈的导航控制器。
代码是这样的:
let buttona = UIBarButtonItem(title: "Buttona", style: .plain, target: self, action: #selector(buttonaToolbar(sender:)));
let buttonb = UIBarButtonItem(title: "Buttonb", style: .plain, target: self, action: #selector(buttonbToolbar(sender:)));
let title = UILabel();
toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 44));
Run Code Online (Sandbox Code Playgroud) 如果在索引 path.item 的单元格中满足某些条件,我想访问集合视图中的前一个单元格(索引 path.item - 1)以更改那里的某些设置
现在我试过这个:
let previousCell = collectionView.cellForItem(at: indexPath - 1)
Run Code Online (Sandbox Code Playgroud)
但它不起作用,因为 - 1 它从索引路径向下转换为整数值。
我怎么得到这个细胞?
我有一个 func,我必须在其中将当前月份和年份参数传递给 Swift 3 中的 Fetch API。代码 :-
func raffleNumberGenerate(){
let prs = [
"month":currentMonth,
"year" : currentYear,
"raffle_result": "1" as String
]
Service.StartWithoutLoading(prs as [String : AnyObject]?, onCompletion: { result in
let jsonResponseSingle = result as? NSDictionary
print(" JSON Response :- \(String(describing: jsonResponseSingle))"
}
Run Code Online (Sandbox Code Playgroud)
提前致谢。
我正在尝试获得类似于 facebook 发布日期/时间的内容。像 1 分钟、1 小时、昨天、一周前等。
这是我用来获取这种格式的函数:( Oct-22 17:28 PM)
func convertTime(serverTime: Double) -> String {
let serverTime = serverTime
let date = Date(timeIntervalSince1970: serverTime)
let dateFomatter = DateFormatter()
dateFomatter.dateFormat = "MMM-dd HH:mm a"
let strDate = dateFomatter.string(from: date)
print("This is the post's date: \(strDate)")
return strDate
}
Run Code Online (Sandbox Code Playgroud) 我有一个带有目标 c 类的视图控制器,现在我想打开包含 swift 类的视图控制器。我已经尝试了几次,但它没有打开 swift 类,我如何从目标 c 类打开 swift 类。
我有以下功能
public func encryptWithRSAKey(_ data: String, rsaKeyRef: SecKey, padding: SecPadding) -> [UInt8]? {
let blockSize = SecKeyGetBlockSize(rsaKeyRef)
var messageEncrypted = [UInt8](repeating: 0, count: blockSize)
var messageEncryptedSize = blockSize
var status: OSStatus!
status = SecKeyEncrypt(rsaKeyRef, SecPadding.OAEP, data, data.count, &messageEncrypted, &messageEncryptedSize)
if status != noErr {
print("\(logClassName): Encryption Error!")
}
return messageEncrypted
}
Run Code Online (Sandbox Code Playgroud)
输出为 encryptedMessage = [9,43,128...] 128 长度
我必须将结果发送到 C 函数中
void STDCALL CpProxyAvOpenhomeOrgCredentials1BeginSet(THandle aHandle, const char* aId, const char* aUserName, const char* aPassword, uint32_t aPasswordLen, OhNetCallbackAsync aCallback, void* aPtr)
{ …Run Code Online (Sandbox Code Playgroud) 当我运行在Xcode iOS和watchOS模拟器的手表连接的应用程序,WCSession委托方法didReceiveApplicationContext仅适用于第一次,但随后它不叫,并在接口控制器没什么变化。谁能解释一下为什么会发生这种情况?
下面是 UIViewController 的 WCSessionVC 类
import Foundation
import UIKit
import WatchConnectivity
class WCSessionVC: UIViewController, WCSessionDelegate {
let session = WCSession.default
override func viewDidLoad() {
super.viewDidLoad()
session.delegate = self
session.activate()
}
func updateApplicationContext(applicationContext: [String : Any]) throws {
if WCSession.default.isPaired {
do {
try WCSession.default.updateApplicationContext(applicationContext)
} catch let error {
throw error
}
}
}
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
print("Session activated")
let message = ["quote": "Hello"]
do {
try self.updateApplicationContext(applicationContext: message …Run Code Online (Sandbox Code Playgroud) 我的代码没问题,但在一些测试用例上保持时间,有什么改进的技巧吗?我的猜测是 indexOf 函数花费的时间太长。
func checkMagazine(magazine: [String], note: [String]) -> Void {
var mutableMag = magazine
if note.count > mutableMag.count {
print("No")
return
}
for word in note {
if let index = mutableMag.index(of: word) {
mutableMag.remove(at: index)
} else {
print("No")
return
}
}
print("Yes") }
Run Code Online (Sandbox Code Playgroud)
请在此链接中找到挑战:https : //www.hackerrank.com/challenges/ctci-ransom-note/problem