小编LC *_*C 웃的帖子

删除发行版iOS Swift的println()

println()如果我不在Debug构建中,我想全局忽略我的Swift代码中的所有调用.我找不到任何有力的分步说明,并希望得到指导.有没有办法在全球范围内做到这一点,还是我需要println()#IF DEBUG/#ENDIF语句包围每一个?

xcode ios swift xcode6

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

如何在swift中以编程方式创建自定义视图,具有控件文本字段,按钮等

我试图MyCustomView使用ViewController.swift中的以下代码从另一个类访问..

var view = MyCustomView(frame: CGRectZero)
Run Code Online (Sandbox Code Playgroud)

..在viewDidLoad方法中.问题是视图没有在模拟器中初始化.

我已经在当前的ViewController的storyboard中设置了类.

class MyCustomView: UIView {
    var label: UILabel = UILabel()
    var myNames = ["dipen","laxu","anis","aakash","santosh","raaa","ggdds","house"]

    override init(){
        super.init()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addCustomView()
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func addCustomView() {
        label.frame = CGRectMake(50, 10, 200, 100)
        label.backgroundColor=UIColor.whiteColor()
        label.textAlignment = NSTextAlignment.Center
        label.text = "test label"
        label.hidden=true
        self.addSubview(label)

        var btn: UIButton = UIButton()
        btn.frame=CGRectMake(50, 120, 200, 100)
        btn.backgroundColor=UIColor.redColor()
        btn.setTitle("button", forState: UIControlState.Normal) …
Run Code Online (Sandbox Code Playgroud)

xcode custom-view ios swift

46
推荐指数
4
解决办法
14万
查看次数

如何使用swift创建带有页面控件的Scroll View?

我正在尝试创建一个具有多个视图的页面视图控制器.我想要一个使用UIPageViewController的简单代码示例.

pagination uiscrollview uipagecontrol ios swift

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

检查一个字符串是否至少包含一个在Swift中的upperCase字母,数字或特殊字符?

我正在尝试创建一个方法,用于查找字符串是否包含数字,大写字母和使用正则表达式的特殊字符,如下所示

 func checkTextSufficientComplexity(var text : String) -> Bool{


            let capitalLetterRegEx  = "[A-Z]+"
            var texttest = NSPredicate(format:"SELF MATCHES %@", capitalLetterRegEx)
            var capitalresult = texttest.evaluateWithObject("AniP")
            println("\(capitalresult)")


            let numberRegEx  = "[0-9]+"
            var texttest1 = NSPredicate(format:"SELF MATCHES %@", numberRegEx)
            var numberresult = texttest1.evaluateWithObject(text)
            println("\(numberresult)")


            let specialCharacterRegEx  = "[.*&^%$#@()/]+"
            var texttest2 = NSPredicate(format:"SELF MATCHES %@", numberRegEx)

            var specialresult = texttest2.evaluateWithObject(text)
            println("\(specialresult)")

           return capitalresult && numberresult && specialresult

    }
Run Code Online (Sandbox Code Playgroud)

问题是下面的正则表达式[AZ] +仅返回true,例如AVATAR,并为Avatar返回false.我希望我的正则表达式返回true,如果它在String中包含至少一个UpperCase.

regex ios swift

16
推荐指数
4
解决办法
2万
查看次数

应用程序在更改权限时崩溃 - 快速

我需要提示用户通过UIAlertController更改我的应用程序的相机权限.警报具有以下操作:

alert.addAction(UIAlertAction(title: "Open Settings", style: .default, handler: { (action) -> Void in

    guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
        return
    }

    if UIApplication.shared.canOpenURL(settingsUrl) {
        DispatchQueue.main.async(execute: {
            UIApplication.shared.openURL(settingsUrl)
        })
    }
}))
Run Code Online (Sandbox Code Playgroud)

这在打开设置时确实有效,但如果用户更改了相机权限,则应用程序会在后台崩溃Message from debugger: Terminated due to signal 9.

他们现在可以打开应用程序并且权限是正确的,但是他们需要从头开始.有谁知道如何解决这个问题?

iphone ios swift

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

在两个ViewControllers(委托)之间传递数据 - Swift

我有两个 ViewController's.

  1. FirstVC- 我有labelbuttonsegue"莫代尔"

  2. SecondVC- 我有(PickerViewbutton回到FirstVC):

    @IBAction func bntback(sender: AnyObject) {
              self.dissmissViewControllerAnimatied(true, completion: nil)
        }
    
    Run Code Online (Sandbox Code Playgroud)

我创建了代表SecondViewController:

protocol SendDataDelegate {
   func sendData(text:String)
}
Run Code Online (Sandbox Code Playgroud)

下一个:

class SecondVC: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
      var delegate: SendDataDelegate!
      var firstvc = FirstVC()
      var arr = ["First", "Second", "Third"]
      @IBOutlet var pickview: UIPickerView!
      override func viewDidLoad() {
         super.viewDidLoad()
         pickview.dataSource = self
         pickview.selegate = self
     }
Run Code Online (Sandbox Code Playgroud)

我的函数PickerView和函数我使用我的委托作为:

func pickerView (pickerView: UIPickerView, didSelectRow row: Int, …
Run Code Online (Sandbox Code Playgroud)

modal-dialog ios segue swift

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

如何保存实现Codable的自定义对象

现在,Swift 4可以更轻松地对JSON或属性列表进行编码/解码.

但是我找不到如何使用Codable对Data进行编码,而不使用Objective-C方法initWithCoderencodeWithCoder.

考虑这个简单的类:

struct Question: Codable {
    var title: String
    var answer: Int
    var question: Int
}
Run Code Online (Sandbox Code Playgroud)

我怎么能对数据进行编码来使用CodingKeys,而不是initWithCoderencodeWithCoder

编辑:我还需要能够使用NSKeyedArchiver反序列化以前保存在userdefaults中的对象.

ios swift swift4

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

Swift 3:AppDelegate不符合协议GIDSignInDelegate

现在我正在尝试实施Google登录.我跟着这样说:https: //developers.google.com/identity/sign-in/ios/sign-in?ver = swift

但我在AppDelegate.swift中遇到错误:

class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate 
....
Run Code Online (Sandbox Code Playgroud)

错误:

类型'AppDelegate'不符合协议'GIDSignInDelegate'

帮我.

swift google-signin swift3

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

CLLocationManager requestWhenInUseAuthorization()不起作用

我试图在我的iOS应用程序中使用位置服务但由于某种原因requestWhenInUseAuthorization无法正常工作.当用户第一次使用该应用程序时,提示按正常方式询问权限,但是当您第二次打开应用程序时,由于某种原因,didChangeAuthorizationStatus方法未被调用,因此我无法在地图上显示用户当前位置.

我的代码如下:

 override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib
    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    var config:NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
    config.URLCache = NSURLCache(memoryCapacity: 2 * 1024 * 1024, diskCapacity: 10 * 1024 * 1024, diskPath: "MarkerData")
    markerSession = NSURLSession(configuration: config)
 }



 func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if status == .AuthorizedWhenInUse {

        locationManager.startUpdatingLocation()
        mapView.delegate = self
        mapView.myLocationEnabled = true
        mapView.settings.myLocationButton = true
     }
 }
Run Code Online (Sandbox Code Playgroud)

core-location cllocationmanager ios swift

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

如何将UiSearchBar的背景颜色更改为黑色-SWIFT

我按照下面的编程方式创建了UISearchBar,按钮看起来是第二个图像.如何清除搜索栏的白色并将背景颜色设置为黑色

var categorySearchBar : UISearchBar = UISearchBar()
        categorySearchBar.frame = CGRectMake(0, 20, self.view.frame.size.width, 30)
        categorySearchBar.layer.borderColor = UIColor.grayColor().CGColor
        categorySearchBar.layer.borderWidth = 1.0
        categorySearchBar.placeholder = "SEARCH"
        categorySearchBar.barTintColor = UIColor.blackColor()
        self.view.addSubview(categorySearchBar)
Run Code Online (Sandbox Code Playgroud)

图1

图2

uisearchbar ios swift

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

'NSPersistentContainer'仅适用于iOS 10.0或更高版本

我是IOS app开发的新手.我开发了一个应用程序,显示用户登录的网站.它适用于部署目标设置为10.1.为了使其与IOS 8兼容,我试图将部署目标设置为8,因为我低于错误.

'NSPersistentContainer' is only available on iOS 10.0 or newer
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我需要更改代码,使其与IOS 8,9和10兼容.Xcode:8.2.1 Swift版本:3.2

import UIKit
import CoreData

 @UIApplicationMain

 class AppDelegate: UIResponder, UIApplicationDelegate {

     var window: UIWindow?


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

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an …
Run Code Online (Sandbox Code Playgroud)

core-data ios swift

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

为什么不调用iOS中的自定义委托

我正在尝试使用swift中的playground创建自定义委托.但是,不通过回调调用doSomething方法.似乎委托?.doSomething()不会触发XYZdoSomething方法.提前致谢!

import UIKit

@objc protocol RequestDelegate
{
    func doSomething();

      optional  func requestPrinting(item : String,id : Int)
}


class ABC
{
    var delegate : RequestDelegate?
     func executerequest() {

        delegate?.doSomething()
        println("ok delegate method will be calling")
    }
}   

class XYZ : RequestDelegate
{  
    init()
    {
        var a  = ABC()
        a.delegate = self
    }

     func doSomething() {
       println("this is the protocol method")
    }
}    

var a = ABC()
a.executerequest()
Run Code Online (Sandbox Code Playgroud)

delegates ios swift swift-playground

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

在 Tableview 中使用 BezierPath 创建时间轴视图

我试图在 TableView 中绘制一个具有 5 行的里程碑,我在单元格中添加了一个标签为“O”,这里的行高是 44,我试图连接这些点像这样的东西 O------O-- ----O------O-----O 垂直,但我无法弄清楚这里缺少什么,请参考以下代码

 class StopslistTableViewCell:UITableViewCell{

@IBOutlet weak var milestoneLbl: UILabel!

override func draw(_ rect: CGRect) {

    let scrrenX = UIScreen.main.bounds.width - 30

    let frametosuperview = milestoneLbl.convert(milestoneLbl!.frame, to: self.superview)

    print("dotframetosuperview",frametosuperview)

    //print(CGPoint(x: scrrenX, y: previousposY + rect.size.height))

    //frametosuperview.origin.y > 40 ? frametosuperview.origin.y - 20 : frametosuperview.origin.y

    let path = UIBezierPath()
    path.move(to: CGPoint(x: scrrenX, y:frametosuperview.origin.y < 30 ? frametosuperview.origin.y : frametosuperview.origin.y - frametosuperview.height))

    print("move to point--->",frametosuperview.origin.y < 30 ? frametosuperview.origin.y : frametosuperview.origin.y - frametosuperview.height)

    path.addLine(to: CGPoint(x: scrrenX, …
Run Code Online (Sandbox Code Playgroud)

ios swift swift3 xcode8

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