小编Big*_*tor的帖子

Swift - 如何为NSUserDefaults设置初始值

我有一个名为soundSwitch的开关,我正在使用用户默认值保存按钮的状态:

@IBAction func soundsChanged(sender: AnyObject) {
        if soundSwitch.on{
            defaults.setBool(true, forKey: "SoundActive")
            print("Sound ON")
        }else{
            defaults.setBool(false, forKey: "SoundActive")
            print("Sound OFF")
        }
    }
Run Code Online (Sandbox Code Playgroud)

当前,当用户首次启动应用程序时,实际默认值最初为false.

如果用户首次启动应用程序并且尚未配置它,我如何将默认值实现为true.

我在Objective-C中看过方法,但在Swift中没有.从我所看到的你可以在app中委托一些方法,或者在plist文件中.我该怎么办?

nsuserdefaults uiswitch ios swift

33
推荐指数
5
解决办法
2万
查看次数

为什么要使用cocoapods?

我一直在尝试将cocoapods用于一些应用程序,每次只是搞砸了我的框架,我发现只需拖动一个框架并直接使用它就会简单得多.完全去除可可豆荚也是如此混乱!

我不能为我的生活理解为什么人们认为cocoapods是任何类型的应用程序的可接受的解决方案.

我不知道也许我没有以正确的方式使用它,但为什么我们一般需要依赖管理器?

我使用过迦太基,它似乎是一个非常优越的替代方案,它可以为您提供框架而无需弄乱您的Xcode项目!

dependencies dependency-management ios cocoapods carthage

6
推荐指数
0
解决办法
533
查看次数

无法将"Range <Int>"类型的值转换为预期的参数类型"Range <Index>"(又名"Range <String.CharacterView.Index>")

我在这里有一个字符串,我正在尝试子串.

let desc = "Hello world. Hello World."
var stringRange = 1..<5
desc.substringWithRange(stringRange)
Run Code Online (Sandbox Code Playgroud)

但是Swift给我一个错误.我做错了什么?我正在使用stringRange的新表示法,因为它不允许我使用旧的.

string substring swift

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

Swift:如何在 NSURL 中引用本地图像

我有一个代码,它从在线检索的文章中加载一些缩略图并将它们放入一个Article对象中。它看起来像下面这样:

            for article in newArticlesArray {
                let url: String
                if let myGroup = article["group"] as? Dictionary<NSObject, AnyObject>, let myThumbnail = myGroup["thumbnail"] as? Dictionary<NSObject, AnyObject>, let myURL = myThumbnail["url"] as? String{
                    url = myURL
                }
                else{
                    url = "file://no-thumbnail.png"
                }

            let a = Article(t: article["title"] as! String,
                                    da: article["pubDate"] as! String,
                                    de: newDesc,
                                    th: NSURL(string: url)!,
                                    l: NSURL(string: article["link"] as! String)!)
                    articlesArray.addObject(a)
Run Code Online (Sandbox Code Playgroud)

但是,当文章没有缩略图时会出现问题,因此我必须使用本地图像。没有缩略图的本地文件被称为no-thumbnail.png,但是我似乎无法在线找到有关如何通过使用 swift 中的 NSURL 实际引用本地文件(在本例中为 .png 图像)的简单指南。

寻找有人分享对此的一些见解。

解决方案

对于有兴趣的人,解决方案如下:

let th = NSBundle.mainBundle().URLForResource("no-thumbnail", withExtension: …
Run Code Online (Sandbox Code Playgroud)

image nsurl local-files swift

5
推荐指数
2
解决办法
5982
查看次数

在Swift 2.0中,字符串的最大长度是多少?

我有一个使用单个字符串的应用程序.此字符串包含从数组加载的数据,然后该字符串将导出到文本文件.

我的问题是这个字符串可能的最长长度是什么,它什么时候变得太长了?

string maxlength string-length ios swift

3
推荐指数
2
解决办法
5849
查看次数

Swift,如何从点击的自定义注释中获取信息

我有以下自定义注释类:

import UIKit
import MapKit

class LocationMapAnnotation: NSObject, MKAnnotation {
    var title: String?
    var coordinate: CLLocationCoordinate2D
    var location: Location

    init(title: String, coordinate: CLLocationCoordinate2D, location: Location) {
        self.title = title
        self.coordinate = coordinate
        self.location = location
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在将注释加载到这样的地图视图中:

for i in 0..<allLocations.count{
            //Add an annotation
            let l: Location = self.allLocations[i] as! Location
            let coordinates = CLLocationCoordinate2DMake(l.latitude as Double, l.longitude as Double)
            let annotation = LocationAnnotation(title: l.name, coordinate: coordinates, location: l)
            mapView.addAnnotation(annotation)
        }
Run Code Online (Sandbox Code Playgroud)

我想Location从选定的注释中获取对象.目前我有这个方法,当我点击注释时调用,但我不确定如何从注释中检索特定对象.

func mapView(mapView: MKMapView, didSelectAnnotationView view: …
Run Code Online (Sandbox Code Playgroud)

annotations mkmapview mkannotationview ios swift

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

Swift如何以编程方式移动UIButton?

我的infoButtonswift文件中有一个UIButton插座。此按钮最初放置在情节提要中时受到以下限制:

约束图片

我想将此按钮下移50个像素,具体取决于NoAds布尔值是对还是对。

我已经尝试过执行此操作,但是似乎无法移动它。

if NoAds{
            print("No Ads")
            infoButton.center.y = infoButton.center.y - 50
        }else{
            print("Ads")
            loadBanner()
        }
Run Code Online (Sandbox Code Playgroud)

我认为这应该很容易解决?

编辑:该广告是标准的Google广告横幅,宽320高50高。

storyboard uibutton uiview ios swift

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

如何比较if语句中的多个变量?

我有多个变量:

  1. var1
  2. var2
  3. var3

如何在一行代码中检查是否有任何变量小于0而不将其分成3个if语句?

variables if-statement var ios

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

DataTables在.php文件中不起作用

你好我有下表,我希望把它变成一个DataTable.

<?php require 'bootstrap.php';?>
  <!doctype html>
  <html>

  <head>
    <meta charset="UTF-8">
    <title>Service Centres</title>
    <link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css">
    <script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.12/js/jquery.dataTables.js"></script>
  </head>

  <body>
    <h1 align="center">Service Centres</h1>
    <table border="1" align="center" id="service_table" class="display">
      <tr>
        <th>Centre Postcode</th>
        <th>Centre Type</th>
      </tr>
      <tr>
        <td>12345</td>
        <td>Standard</td>
      </tr>
      <tr>
        <td>12345</td>
        <td>Standard</td>
      </tr>
      <tr>
        <td>12345</td>
        <td>Standard</td>
      </tr>
    </table>
    <script type="text/javascript">
      $(document).ready(
        function() {
          $('#service_table').DataTable();
        });

    </script>
  </body>

  </html>
Run Code Online (Sandbox Code Playgroud)

但由于某些原因,这不起作用.如果我不包含脚本部分,我会注意到我的表更改,但它没有任何排序/搜索功能,并且由于某种原因变得像整个屏幕一样宽.

有人知道如何解决这个问题吗?

这里是小提琴:https://jsfiddle.net/6pye363h/1/

javascript php jquery datatables

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