小编D4t*_*aya的帖子

如何正确处理带参数的Swift块中的弱自我

在我TextViewTableViewCell,我有一个变量来跟踪块和一个配置方法,其中传入和分配块.
这是我的TextViewTableViewCell班级:

//
//  TextViewTableViewCell.swift
//

import UIKit

class TextViewTableViewCell: UITableViewCell, UITextViewDelegate {

    @IBOutlet var textView : UITextView

    var onTextViewEditClosure : ((text : String) -> Void)?

    func configure(#text: String?, onTextEdit : ((text : String) -> Void)) {
        onTextViewEditClosure = onTextEdit
        textView.delegate = self
        textView.text = text
    }

    // #pragma mark - Text View Delegate

    func textViewDidEndEditing(textView: UITextView!) {
        if onTextViewEditClosure {
            onTextViewEditClosure!(text: textView.text)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我在我的cellForRowAtIndexPath方法中使用configure方法时,如何在我传入的块中正确使用弱自我.
这是我没有弱自我的情况:

let myCell = tableView.dequeueReusableCellWithIdentifier(textViewCellIdenfitier) as TextViewTableViewCell …
Run Code Online (Sandbox Code Playgroud)

ios retain-cycle swift

143
推荐指数
7
解决办法
11万
查看次数

无法为桥接标头发出预编译的标头

我从GitHub下载了一个项目,然后pod下面的文件,其中一些是由OBJ-C编写的,我使用了一个桥头.

pod ‘SnapKit’
pod ‘MJRefresh’
pod ‘Alamofire’
pod ‘Kingfisher’
pod ‘MBProgressHUD’
pod ‘pop’
pod ‘EVReflection’
pod ‘StreamingKit’
pod ‘iCarousel’
pod ‘ReflectionView’
Run Code Online (Sandbox Code Playgroud)

当我使用Xcode 9.0 beta 2运行项目时,遗憾的是错误日志如下:

错误:未能发出预编译头'/var/folders/kd/4gh0_kxx3jx4thjb_sssmmcw0000gn/T/EvoRadio-Bridging-Header-97bd5f.pch'用于桥接标题'/ Users/ringo/Downloads/EvoRadio-master/EvoRadio/Resources/EvoRadio-桥接-Header.h"

我用google搜索,但没有这样的问题.错误意味着它需要一个PCH文件?这是我的.pch标题配置: 在此输入图像描述 它解决不了.

怎么做?

compiler-errors ios swift objective-c-swift-bridge

27
推荐指数
8
解决办法
3万
查看次数

ios swift 3 xcode8 beta圆形imageView

在我的项目中,我使用以下代码以使我的图像呈圆形:

profileImage.layer.cornerRadius = profileImage.frame.size.width / 2
profileImage.clipsToBounds = true
Run Code Online (Sandbox Code Playgroud)

我也使用对比来制作它width = hight和其他约束.

在将我的项目升级到xcode 8 beta和swift 3之后,我设置为舍入的所有图像视图都消失了,当我删除代码以使其四舍五入或删除所有约束时它们再次出现.
但我仍然需要它们四舍五入.任何人都可以帮我解决问题.谢谢

ios swift3

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

如何在命令行上解密加密的sqlcipher数据库文件?

问题很简单

我有的是:

  • 我有一个使用sqlcipher 加密数据库文件.
  • 我也用于加密这个db文件的密码

我需要的是:

  • 我需要解密数据库文件 /需要一个未加密/非加密/解密的数据库文件.

encryption terminal sqlcipher

16
推荐指数
3
解决办法
5万
查看次数

快速关闭 - 将自我捕捉为弱者

我试图在Swift中解决基于闭包的强引用循环.
在下面的代码中,对象由拥有的视图控制器保留.ProgressHUD是一个UIView由拥有的视图控制器保留的.ProgressHUD每次调用完成处理程序时都会泄露.使用新的闭包捕获功能时,将self声明为weak或unowned并不能解决内存泄漏问题.

object.setCompletionHandler { [weak self] (error) -> Void in
    if(!error){
        self?.tableView.reloadData()
    }
    self?.progressHUD?.hide(false)
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我在闭包之外为self声明一个弱var,它会修复内存泄漏,如下所示:

weak var weakSelf = self
object.setCompletionHandler { (error) -> Void in
    if(!error){
        weakSelf?.tableView.reloadData()
    }
    weakSelf?.progressHUD?.hide(false)
}
Run Code Online (Sandbox Code Playgroud)

关于为什么这不适用于Swift捕获的任何想法?

closures weak-references swift

13
推荐指数
3
解决办法
8841
查看次数

如何在Xcode中左对齐导航栏的标题?

我一直在尝试以下方法,以使导航栏的标题保持左对齐:

AppDelegate.swift文件中:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.


    UINavigationBar.appearance().barTintColor = UIColor.red
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:
        UIColor.white]
    return true
}
Run Code Online (Sandbox Code Playgroud)

TableViewController.swift文件中:

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.navigationController?.navigationBar.topItem?.title = "Home"
    }
Run Code Online (Sandbox Code Playgroud)

但我找不到任何解决问题的方法.我还尝试了以下我在这里找不到的内容:

AppDelegate.swift文件中:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.


    UINavigationBar.appearance().barTintColor = UIColor.red
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:
        UIColor.white]
let lbNavTitle = …
Run Code Online (Sandbox Code Playgroud)

xcode uinavigationbar swift

10
推荐指数
4
解决办法
1万
查看次数

在swift词典中找到一个包含字符串的键

我有一[String:String]本字典.我想获得与包含字符串"S"的任何键相关联的值.订单无关紧要.

这很简单:只需获取所有键,迭代,返回第一个匹配条件.

但是,我想用一种快速优雅的方法来做到这一点.使用a filtermap函数的东西.这就是我迷路的地方......

dictionary swift

8
推荐指数
3
解决办法
4008
查看次数

如何使用swift上传带云套件的图片?

如何使用swift上传和加载来自云套件的图像?

我使用什么属性类型?

在此输入图像描述

我用什么代码?这是我目前使用的代码......

func SaveImageInCloud(ImageToSave: UIImage) {
        let newRecord:CKRecord = CKRecord(recordType: "ImageRecord")
        newRecord.setValue(ImageToSave, forKey: "Image")

        if let database = self.privateDatabase {


            database.saveRecord(newRecord, completionHandler: { (record:CKRecord!, error:NSError! ) in

                if error != nil {

               NSLog(error.localizedDescription)

                }


                else {

                    dispatch_async(dispatch_get_main_queue()) {

println("finished")

                    }
                }
        })
        }
Run Code Online (Sandbox Code Playgroud)

xcode ios icloud swift cloudkit

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

键盘高度变化观察者迅速

如何在iOS swift中检测键盘高度变化或键盘更改?
看下面的代码,它在文本键盘和Emojis键盘中显示非常小的一行:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardHideNShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardHideNShow(_:)), name: UIKeyboardWillHideNotification, object: nil)
var didDisplayedKeyboard:Bool = false

func keyboardHideNShow(notification:NSNotification) {

var movement: CGFloat = 0.0
let userInfo:NSDictionary = notification.userInfo!
let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.CGRectValue()

let movementDuration:NSTimeInterval = 0.3
UIView.beginAnimations( "animateView", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration )

if notification.name == UIKeyboardWillShowNotification {
    // Do the operation only if its hide n show or show n hide
    // When the keyboard switches from text …
Run Code Online (Sandbox Code Playgroud)

keyboard swift

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

URLSession.datatask,后台没有调用请求块

URLSession当应用程序处于后台时,数据任务块没有调用,并且它仍然处于dataTask请求状态.
当我打开应用程序时,块被调用.顺便说一句,我正在使用https请求.
这是我的代码:

    let request = NSMutableURLRequest(url: URL(string: url as String)!,

                                      cachePolicy: .reloadIgnoringCacheData,

                                      timeoutInterval:20)

    request.httpMethod = method as String

    request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")

    let session = URLSession.shared

    let data = params.data(using: String.Encoding.utf8.rawValue)

    request.httpBody = data

    session.dataTask(with: request as URLRequest,completionHandler:

        {(data, response, error) -> Void in

         if error == nil

            {

                do {

                    let result = try JSONSerialization.jsonObject(with: data!, options:

                        JSONSerialization.ReadingOptions.mutableContainers)

                    print(result)

                     completionHandler(result as AnyObject?,nil)

                }

                catch let JSONError as NSError{

                    completionHandler(nil,JSONError.localizedDescription as NSString?)

                }

            }

            else{ …
Run Code Online (Sandbox Code Playgroud)

xcode ios nsurlsession swift xcode8

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