标签: swift4

如何为 PDFAnnotation ios swift 4 添加 GestureRecognizer

可以添加 GestureRecognizer 或设置触摸 PDFAnnotation

  func setDocumentAnnotation() {
    let anotation:PDFAnnotation = PDFAnnotation(bounds: CGRect(x: pointX, y: pointY, width: pointW, height: pointH), forType: .highlight, withProperties: nil)
        anotation.color = .yellow
        anotation.endLineStyle = .circle
        guard let page = pdfView.currentPage else {return}
        page.addAnnotation(anotation)
}


 @objc func annotationTapping(_ sender: UITapGestureRecognizer){
    print("------- annotationTapping ------")
}
Run Code Online (Sandbox Code Playgroud)

pdfkit pdf-annotations swift4 ios11

1
推荐指数
1
解决办法
1076
查看次数

使用 Swift 4 Decodable 解析具有混合属性值的 JSON

我正在尝试更新我的应用程序以使用 Swift 4 Decodable - 并且正在从具有子值的 JSON api 中提取数据,这些子值可能是:

  • 一个子对象
  • 一个字符串
  • 一个整数

这是 Json Api 响应:

var jsonoutput =
"""
{
"id": "124549",
"key": "TEST-32",
"fields": {
"lastViewed": "2018-02-17T21:40:38.864+0000",
"timeestimate": 26640
}
}
""".data(using: .utf8)
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下方法解析它:如果我只引用都是字符串的 id 和 key 属性,这会起作用。

struct SupportDeskResponse: Decodable{
    var id: String
    var key: String
    //var fields: [String: Any] //this is commented out as this approach doesn't work - just generated a decodable protocol error.            
}

var myStruct: Any!
do {
    myStruct = try JSONDecoder().decode(SupportDeskResponse.self, from: …
Run Code Online (Sandbox Code Playgroud)

json swift swift4 decodable

1
推荐指数
1
解决办法
154
查看次数

将启用时间限制为 UIDatePicker swift 4 中的特定时间跨度

我试图将 datePicker 的时间限制为从 09:00 AM 到 09:00 PM(21:00),时间间隔为 30 分钟。为此,我尝试了所有已知的方法,并在互联网上搜索了很长时间。但我什么也没发现。这是我写的代码

//datePicker.setDate(dateFormatter.date(from: "09.00")!, animated: true)
datePicker.maximumDate = Calendar.current.date(byAdding: .hour, value: -12, to: dateFormatter.date(from: "21.00")!) //)Calendar.current.date(byAdding: Calendar.Component.hour, value: 1, to: Date())
 datePicker.minimumDate = Calendar.current.date(byAdding: Calendar.Component.hour, value: -12, to: dateFormatter.date(from: "09.00")!)
Run Code Online (Sandbox Code Playgroud)

但它没有用。请帮忙。

xcode ios swift swift4

1
推荐指数
1
解决办法
5470
查看次数

将 arkit 快照直接保存到照片库 swift 4

下面的代码部分拍摄了 arkit 的快照并将其放置在图像视图上。我正在尝试将其剪掉,然后从拍摄照片并自动将其保存到照片库中,而无需先将其放置在图像视图中。

    @IBOutlet var cryMeARiver: UIImageView!
@IBOutlet weak var augmentedRealityView: ARSCNView!

 @IBAction func place(_ sender: Any) {
//THIS LINE OF THE CODE BELOW SAVES THE SNAPSHOT TO THE IMAGEVIEW
cryMeARiver.image = augmentedRealityView.snapshot()
}
Run Code Online (Sandbox Code Playgroud)

save photo-gallery ios swift4 arkit

1
推荐指数
1
解决办法
1799
查看次数

“此时无法安装此应用程序”CFBundleIdentifier 错误

我遇到了“此时无法安装此应用程序”错误。我尝试了几种方法,例如重新加载模拟器,删除模拟器中的应用程序和清理Xcode项目。但是,它仍然存在此错误。我转向 CoreSimulator 文件,发现由于 CFBundle Identifier 导致的错误。这是下面的日志:

Apr 26 18:49:49 kevins-air com.apple.dt.Xcode[27210] <Error>: installApplication:withOptions:error:: Error Domain=IXUserPresentableErrorDomain Code=1 "This app could not be installed at this time." UserInfo={NSLocalizedDescription=This app could not be installed at this time., NSUnderlyingError=0x7faad5709050 {Error Domain=MIInstallerErrorDomain Code=12 "Bundle at path /Users/kevinlauofficial/Library/Developer/CoreSimulator/Devices/0EB4D69B-61C8-468B-9D3D-2D6761E1D9D6/data/Library/Caches/com.apple.mobile.installd.staging/temp.IMKy1v/extracted/DBS.app/Frameworks/HandySwift.framework did not have a CFBundleIdentifier in its Info.plist" UserInfo={LegacyErrorString=MissingBundleIdentifier, FunctionName=-[MIBundle _validateWithError:], SourceFileLine=45, NSLocalizedDescription=Bundle at path /Users/kevinlauofficial/Library/Developer/CoreSimulator/Devices/0EB4D69B-61C8-468B-9D3D-2D6761E1D9D6/data/Library/Caches/com.apple.mobile.installd.staging/temp.IMKy1v/extracted/DBS.app/Frameworks/HandySwift.framework did not have a CFBundleIdentifier in its Info.plist}}}
Run Code Online (Sandbox Code Playgroud)

抱歉,我无法将其转换为代码格式。这是一个屏幕截图: 错误截图

DBS.app/Frameworks/HandySwift.framework 在其 Info.plist 中没有 CFBundleIdentifier}}} 表明 HandySwift.framework 在其 Info.plist 中没有 CFBundleIdentifier。我应该在属性列表中添加什么?这是我的 Info.plist 的屏幕截图。 …

xcode cfbundleidentifier ios swift swift4

1
推荐指数
1
解决办法
8857
查看次数

在 Swift 4 中从字符串对象创建 CFData 对象

根据 GDPR 要求,我使用 Apple 的内置“安全”模块来帮助加密和解密信息。我能够使用文档相当轻松地生成我的私钥和公钥。

我现在发现自己的问题是尝试进行加密时。在我正在关注的 Apple 文档(在此处找到)中,我能够进入 SecKeyCreateEncryptedData 函数步骤。我在说“plainText as CFData”的行中遇到错误。简单地说,我从 UITextField 接收一个 String 对象,并通过在我的 UITextField 对象之后添加“.text”来正确检索输入文本。

当你这样做时,Xcode 尖叫着必须使用“as!”强制向下转换一个 String 对象。如果我们想要它作为 CFData。所以我遵守了Xcode,都看到这行不通。我在文档说“plainText as CFData”的行上得到“Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)”。当我通过调试器查看它时,它是一大堆程序集,老实说,这超出了我的范围。

无论如何,这让我感到困惑,因为如果 String 不是文档所指的纯文本,那么什么是?我的直觉只是告诉我对 Swift 中的 CFData 是什么做一些研究,这样我就可以将我的 String 对象转换/创建到一个 CFData 对象中,然后简单地将它传递到参数中,而不需要在函数中转换它。那个搜索把我带到了这里,然后我找到了我认为是一个很好的解决方案在这里. 现在,我可能错了,这是一个很好的解决方法,但目前我能想到的只有这些。我现在被困在试图弄清楚我传递给 CFDataCreate 函数的参数是什么。我了解这些类型,但实际上没有任何示例(甚至在文档中也没有),我只是不知道如何获取它,或传入什么,以及如何以它需要的正确格式获取我的 String 对象.

感谢您的帮助,如果您对我有任何疑问,请提问

xcode ios swift swift4

1
推荐指数
1
解决办法
3848
查看次数

swift 4.0 中的故事板本地化

我想用 swift 语言实现故事板本地化。(意味着我想本地化以修复标签和按钮文本)

我已经知道 NSLocalizedString 但我不想编码修复文本标签

例如

NSLocalizedString("Welcome", comment: "")
Run Code Online (Sandbox Code Playgroud)

我已经为特定语言添加了 Localizable.strings 文件和 Main.string 文件。但我无法成功实施本地化

localization nslocalizedstring ios swift swift4

1
推荐指数
1
解决办法
7465
查看次数

Xcode 9 中的 Swift 4 - 如何轻量级核心数据迁移?

我的核心数据将再更新一个属性,为了避免崩溃,我首先添加了一个新的模型版本,此外:

关于这个问题的大部分关键是改变:

coordinator!.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)
Run Code Online (Sandbox Code Playgroud)

选项:在代码中为零

options:[NSMigratePersistentStoresAutomaticallyOption:true, NSInferMappingModelAutomaticallyOption: true]
Run Code Online (Sandbox Code Playgroud)

但是在我的appdelegate.swift 中,我找不到任何“persistentStoreCoordinator”,那么我可以在我的版本中迁移 CoreData 吗?

xcode core-data swift swift4 ios11

1
推荐指数
2
解决办法
1655
查看次数

如何弹出视图控制器然后显示 UIAlertController

假设 ViewController2 中有错误,我想回到 ViewController1,前一个视图控制器,然后显示警报。现在如果我把它放在 ViewController2 中

    @IBAction func testing(_ sender: Any) {
    navigationController?.popViewController(animated: true)
    Alert.alert(userTitle: "Error", userMessage: " ", userOptions: " ", in: LandingViewController() as UIViewController)

}
Run Code Online (Sandbox Code Playgroud)

使用它作为警报类

    public class Alert {
    class func alert(userTitle: String?, userMessage: String, userOptions: String, in vc: UIViewController) {
        DispatchQueue.main.async {
            let alert = UIAlertController(title: userTitle, message: userMessage, preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: userOptions, style: UIAlertActionStyle.default, handler: nil))
            vc.present(alert, animated: true, completion: nil)
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

它会抛出错误

那么有没有办法做到这一点?

swift uialertcontroller swift4

1
推荐指数
1
解决办法
669
查看次数

Cocoa Xcode:使用 Swift 4 for Mac 以编程方式从 AppDelegate 打开窗口控制器?

我正在制作一个简单的菜单栏应用程序,它有 2 个项目 -首选项退出

我想在单击首选项时打开一个新窗口

目前我有

func applicationDidFinishLaunching(_ aNotification: Notification) {
        constructMenu()
    }

func constructMenu() {
        let menu = NSMenu()

        menu.addItem(NSMenuItem(
                       title: "Preferences...", 
                       action: #selector(AppDelegate.preferencesWindow(_:)), 
                       keyEquivalent: "P"))
        menu.addItem(NSMenuItem.separator())
        menu.addItem(NSMenuItem(
                       title: "Quit", 
                       action: #selector(NSApplication.terminate(_:)), 
                       keyEquivalent: "q"))

        statusItem.menu = menu
    }

@objc func preferencesWindow(_ sender: Any) {
        print("open preference window here")
        if let storyboard = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil) {
            let controller = storyboard.instantiateControllerWithIdentifier("preferencesWindow")
 as NSViewController

            if let window = NSApplication.shared.mainWindow {
                window.contentViewController = controller // …
Run Code Online (Sandbox Code Playgroud)

macos xcode cocoa swift swift4

1
推荐指数
1
解决办法
1292
查看次数