小编Hon*_*ney的帖子

仪器之间的差异(僵尸和泄漏)

我现在正在管理我的新应用程序的"记忆",但我发现了一个我不明白的区别.看看这张照片:

区别

分配曲线完全不相似.它来自Zombie模式和Leaks分析器.但为什么它不同,因为曲线似乎来自分配?

谢谢 !

memory memory-management objective-c nszombie ios

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

为什么我的UILocalNotification没有播放任何声音?

这让我疯了,我相信我现在已经尝试了所有可能的方法,但我的手机仍然决定保持沉默.当我提出本地通知时,我只想让它播放任何声音.目前,UILocalNotificationDefaultSoundName我添加到我的应用包中的自定义声音和自定义声音都不起作用.我写了一个简单的倒计时应用程序,presentLocalNotification:在倒计时结束时使用.

- (void)showLocalNotification
{
   UILocalNotification *alarmNotification = [[UILocalNotification alloc] init];
   alarmNotification.alertBody = [NSString stringWithFormat:@"fired at: %@", [NSDate date]];
   alarmNotification.soundName = UILocalNotificationDefaultSoundName; 
   // alarmNotification.soundName = @"glass.aiff"; // "glass.aiff" is a file in my application bundle
   [[UIApplication sharedApplication] presentLocalNotificationNow:alarmNotification];
}
Run Code Online (Sandbox Code Playgroud)

我还会显示一个UIAlertView将从AppDelegate收到通知时初始化并显示的内容:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
   UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"alert"  message:@"Local notification was received" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
   [alertView show];
}
Run Code Online (Sandbox Code Playgroud)

收到本地通知并显示警报,在通知中心我也可以看到通知已被触发,只有声音不起作用...

我检查了堆栈溢出中的每个类似问题,启用了我的所有声音设置,确保在启用应用程序的电话通知设置时,我甚至在我测试声音播放的位置时收到了来自其他应用程序的推送通知. ..

我到底错过了我的手机什么都没发出声音?当我触发通知或收到通知时,是否必须导入一些框架或调用其他任何内容?

也许最简单的解决方案是,如果有人在发布通知时发布播放默认声音所需的代码的最小版本(尽管我相信我自己已经编写了最小版本 - 除了它不起作用). …

audio objective-c ios uilocalnotification

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

iOS - APNS 如何在有效负载中使用 loc_key

在让 APNS 向设备发送消息之前,我想要本地化一个消息字符串。我希望我能看到 json 有效负载本身,让我理解结构,而不是设备为我解析它。但无论如何,我如何使用 loc_key 和 loc_args 参数?从我发现的文档中:

\n\n
\n

loc-key\n string\n 当前本地化的 Localized.strings 文件中警报消息字符串的键(由用户\xe2\x80\x99s 语言首选项设置)。键字符串可以使用 %@ 和 %n$@ 说明符进行格式化,以获取 loc-args 数组中指定的变量。有关详细信息,请参阅本地化格式化字符串。

\n\n

loc-args 字符串数组 出现在 loc-key 中格式说明符位置的变量字符串值。有关更多信息,请参阅本地化格式化字符串。

\n
\n\n

官方文档在这里

\n\n

我需要一个如何本地化字符串的具体示例以及什么是 loc_args ?

\n\n

我只是在这里猜测,但如果我有一个像这样本地化的字符串:\nmys​​tring="cool localized message"\n那么 loc_key 将是“mystring”,对吗?

\n

localization apple-push-notifications ios

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

Swift字符串与字符串!与字符串?

我已经阅读了这个问题以及其他一些问题.但它们与我的问题有点无关

对于UILabel如果不指定?!你会得到这样的错误:

@IBOutlet属性具有非可选类型'UILabel'

然后,Xcode为您提供2个选项来修复它,您可以这样做:

修复它添加?形成可选类型的UIlabel?
修复它添加!形成隐式解包的可选类型UIlabel?

然而,对于字符串你可以只需键入string?!你不会得到一个错误,这是为什么?

如果name没有设置会怎么样?然后我们会有一个nil不使用?,!Swift 都是关于满足'类型安全'吗?

例:

struct PancakeHouse {
  let name: String // this doesn't have '?' nor '!'
  let photo: UIImage?
  let location: CLLocationCoordinate2D?
  let details: String
}
Run Code Online (Sandbox Code Playgroud)

我的专业是confussion时,我们会想使用可选?

optional type-safety swift

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

为什么我会收到错误"Protocol ...只能用作通用约束,因为它有Self或相关的类型要求"?

我写了一个扩展名Int如下.

extension Int {
    func squared () -> Int {
        return self * self
    }
}

print(10.squared()) // works
Run Code Online (Sandbox Code Playgroud)

上面的代码有效.现在我想扩展IntegerType协议,以便Int,UInt,Int64等都符合.我的代码如下.

extension IntegerType {

    func squared () -> IntegerType { // this line creates error

        return self * self

    }
}
Run Code Online (Sandbox Code Playgroud)

我收到错误:

协议'IntegerType'只能用作通用约束,因为它具有Self或关联类型要求

我已经看到了这个问题及其视频和这个问题,仍然无法理解.我只知道associatedType在这种情况下有一些Self但是无法连接点.我觉得我对这个Generics问题缺乏了解也是一个原因......

有人可以详细说明这个主题,为什么扩展会产生错误?

generics protocols self swift protocol-extension

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

Swift"h"必须绑定在每个模式错误中 - 切换问题

switch语句中的这个错误的原因是什么?"h必须绑定在每个模式中"?

我基本上试图将h用作小时变量,确保它不是nil(因为小时最初是一个可选值,然后看它是否大于17).我知道我在某个地方做错了什么,但那个讨厌的小'必须在每个模式中绑定'错误是什么?

谢谢

let date = NSDate()
let calendar = Calendar.current
let components = calendar.dateComponents([.hour], from: date as Date)
let hour = components.hour


switch hour {
case let h, (h as Int) != nil, h >= 17:
    return true
default:
    return false        
}
Run Code Online (Sandbox Code Playgroud)

pattern-matching switch-statement swift

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

如何以编程方式更改我的 viewController 的视图?

我可以使用 Storyboard 更改 UIViewController 中的视图类,如下所示: 在此处输入图片说明

如何以编程方式实现相同的效果?我试过这个:

convenience init() {
    print("convenience init")
    self.init(nibName: nil, bundle: nil)
    self.view = MTKView()
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用。似乎改变了 MTKView 的视图,但框架不会像故事板那样调整大小。

uiviewcontroller uiview ios swift

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

Swift中的动态/运行时调度,或"奇怪的结构行为在一个人看来"

我不是Swift的新手,也不是Objective-C的新手,但是我Error今天看到一些奇怪的行为,这使得我更深入地挖掘了一个子类型.

使用NSString子类时(是的,下面的示例函数类似于基于的类NSObject):

import Foundation

// Class version
class OddString : NSString {
    override var description: String {
        return "No way, José"
    }
}

let odd = OddString()

func printIt(_ string: NSString) {
    print(string.description)
}

print(odd.description)
printIt(odd)
Run Code Online (Sandbox Code Playgroud)

我看到了我期望看到的东西:

No way, José
No way, José
Run Code Online (Sandbox Code Playgroud)

但是,当我使用struct()编写(我认为是)等效代码时Error:

import Foundation

// Struct version
struct TestError : Error {
    var localizedDescription: String {
        return "I am a TestError"
    }
}

let explosive = …
Run Code Online (Sandbox Code Playgroud)

error-handling struct dynamic nslocalizedstring swift

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

为什么只调用我的上一个本地通知功能?

我对swift很新,我正在尝试调用多个函数来请求UISwitch IBAction中的本地通知.我想在某个日期发送通知 - 一年中的每个季度在第4,7,10,1个月.只有第四季度的功能才被调用.如何调用所有四个函数?这是我的代码:

// UISwitch用于季度通知

@IBAction func quarterlyFrequencyTapped(_ sender: UISwitch) {

if quarterlyFrequency.isOn == true {

    firstQuarter(); secondQuarter(); thirdQuarter(); fourthQuarter()

    print("quarterly frequency is \(quarterlyFrequency.isOn)")

} else {

    removeQuarterlyNotification()

    print("quaterly frequency is \(monthlyFrequency.isOn)")

       }

}
Run Code Online (Sandbox Code Playgroud)

//所有四个季度的功能

func firstQuarter() {
    let firstQuarterContent = UNMutableNotificationContent()
    firstQuarterContent.title = "First Quarter"
    firstQuarterContent.subtitle = "Some string"
    firstQuarterContent.body = "Some other string"

    var firstQuarterDate = DateComponents()
    firstQuarterDate.month = 3
    firstQuarterDate.day = 11
    firstQuarterDate.hour = 19
    firstQuarterDate.minute = 20

    let firstQuarterTrigger = UNCalendarNotificationTrigger(dateMatching: firstQuarterDate, repeats: true)
    let …
Run Code Online (Sandbox Code Playgroud)

notifications swift usernotifications

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

无法从通知操作处理程序打开URL

我试图通过在iOS 10/Swift 3上打开URL来处理本地通知.我有一个URL分配给通知的默认操作,另一个分配给自定义操作按钮.

触发默认操作(点击通知本身)后,应用程序会成功打开URL.但是当点击操作按钮时,UNUserNotificationCenterDelegate调用处理程序并canOpenURL返回true,但是打开URL的调用失败.

仅当应用程序在后台或在点击通知时终止时才会出现此问题.如果应用程序位于前台,则两种情况都有效.

我的app delegate application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:])方法中也有一个断点.它命中默认操作但不支持自定义操作.

处理自定义操作时,是否需要执行不同的操作?

这是代码:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
    switch response.actionIdentifier {
    case UNNotificationDefaultActionIdentifier:
        if let link = URL(string: "myapp://default"),
                    UIApplication.shared.canOpenURL(link) {
                    UIApplication.shared.open(link) { result in
                        print("open url result: \(result)") // this works!
            }
        }

    case "customActionIdentifier":
        if let link = URL(string: "myapp://custom"),
                    UIApplication.shared.canOpenURL(link) {
                    UIApplication.shared.open(link) { result in …
Run Code Online (Sandbox Code Playgroud)

openurl ios uilocalnotification ios10 unusernotificationcenter

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