我正在尝试将Swift中的Today Extension添加到我的Objective-C应用程序中.我在调试器日志中不断收到此消息:Failed to inherit CoreMedia permissions from 3005: (null).数字ex.3005每次都不同.我正在从小部件中读取NSUserDefaults,但我正在应用程序本身阅读/写作.我的TodayViewController中唯一的代码如下:
override func viewDidLoad() {
super.viewDidLoad()
let formatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
totalLabel.text = formatter.stringFromNumber(0)
coinsLabel.text = formatter.stringFromNumber(0)
formatter.maximumFractionDigits = 0
billsLabel.text = formatter.stringFromNumber(0)
}
func widgetMarginInsetsForProposedMarginInsets(defaultMarginInsets: UIEdgeInsets) -> UIEdgeInsets {
return UIEdgeInsetsMake(8.0, 16.0, 8.0, 16.0)
}
func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)!) {
// Perform any setup necessary in order to update the view.
// If an error is encountered, use NCUpdateResult.Failed
// If there's no update required, use …Run Code Online (Sandbox Code Playgroud) 我正在使用Swift编写应用程序,我需要显示警报.该应用必须兼容iOS 7和iOS 8.由于UIAlertView已被替换UIAlertController,如何在UIAlertController不检查系统版本的情况下检查是否可用?我听说Apple建议我们不要检查设备的系统版本,以确定API的可用性.
这就是我在iOS 8中使用的,但是在iOS 7上用" dyld: Symbol not found: _OBJC_CLASS_$_UIAlertAction" 崩溃了:
let alert = UIAlertController(title: "Error", message: message, preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
alert.addAction(cancelAction)
presentViewController(alert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
如果我使用UIAlertView for iOS 8,我收到此警告: Warning: Attempt to dismiss from view controller <_UIAlertShimPresentingViewController: 0x7bf72d60> while a presentation or dismiss is in progress!
如果我为UITableView声明了一个NSIndexPath常量,那么使用==运算符进行比较是否有效?
这是我的不变声明:
let DepartureDatePickerIndexPath = NSIndexPath(forRow: 2, inSection: 0)
Run Code Online (Sandbox Code Playgroud)
然后我的功能:
override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
var height: CGFloat = 45
if indexPath == DepartureDatePickerIndexPath{
height = departureDatePickerShowing ? 162 : 0
} else if indexPath == ArrivalDatePickerIndexPath {
height = arrivalDatePickerShowing ? 162 : 0
}
return height
}
Run Code Online (Sandbox Code Playgroud)
这当然可以正常工作,但这样做是否安全?我假设因为它工作,==NSIndexPath对象上的运算符正在比较section和row属性而不是实例.
我希望这个网站以前没有被问过。我无法从谷歌找到可靠的答案。
在项目的多个文件中#include iostream 时会发生什么?我总是在我的头文件中使用 #ifndef 和 #define。这是否会阻止 iostream 被多次包含?
如何使用 Swift 将 UITextField 限制为一位小数?文本字段用于输入价格,因此我不能允许超过一位小数。如果我使用 Objective-C,我会使用以下代码:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSArray *sep = [newString componentsSeparatedByString:@"."];
if([sep count] >= 2)
{
NSString *sepStr=[NSString stringWithFormat:@"%@",[sep objectAtIndex:1]];
return !([sepStr length]>1);
}
return YES;
}
Run Code Online (Sandbox Code Playgroud)
但由于 Swift 使用范围的方式不同,我无法将此代码转换为 Swift。第一行给了我一个错误说NSRange is not convertible to Range<String.Index>
编辑:在看到答案之前我最终这样做了:
func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool {
let tempRange = textField.text.rangeOfString(".", options: NSStringCompareOptions.LiteralSearch, range: nil, locale: nil)
if tempRange?.isEmpty == false && string == …Run Code Online (Sandbox Code Playgroud) 我试图通过计算开始和结束索引将数组分成 n 个相等的部分。开始和结束元素的地址将被传递到一个对这些数组进行排序的函数中。例如,如果 arraySize = 1000,并且 n=2,则索引将为 0、499、999。到目前为止,我有以下代码,但对于奇数 n,它将其拆分为多个数组。我想到的另一种方法是通过循环运行 n 次,但我不确定从哪里开始。
int chunkSize = arraySize / numThreads;
for (int start = 0; start < arraySize; start += chunkSize) {
int end = start + chunkSize - 1;
if (end > arraySize - 1) {
end = arraySize - 1;
}
InsertionSort(&array[start], end - start + 1);
}
Run Code Online (Sandbox Code Playgroud)
编辑:这是我想出的其他东西。它似乎有效,但我需要做一些更彻底的测试。我已经多次绘制并手动追踪它。希望没有任何边缘情况会失败。我已经在限制 n >= arraySize。
int chunkSize = arraySize / numThreads;
for (int i = 0; i < numThreads; i++) {
int start …Run Code Online (Sandbox Code Playgroud) 我有一个通用的iPhone/iPad应用程序,我正在使用AdMob Mediation.我想首先请求iAd,然后在iAd失败时请求AdMob.我的iAd eCPM设置为1.00美元,AdMob设置为0.20美元.但是,我的iAd填充率约为20%.我已经尝试了许多不同的eCPM设置而没有成功.在最近的应用更新中集成AdMob Mediation之前,我只使用了iAd并获得了大约99%的iAd填充率.
这是因为我使用智能横幅吗?据我了解,iAd确实支持Smart Banner.我的应用程序仅支持纵向模式,智能横幅的尺寸为iPhone的320x50和iPad的768x90.但是,根据Apple的iAd编程指南,iPad的iAd为768x66.
智能横幅尺寸不正确会导致iPad的iAd失败吗?根据我的iTunes销售报告,我有比iPhone用户更多的iPad用户.这个问题的最佳解决方案是什么?我应该停止使用智能横幅,还是应该以编程方式更改GADBannerView的大小(如果该设备是iPad?
我需要将 k (1 <= k <= 16) 个排序数组合并为一个排序数组。这是一个家庭作业,教授要求使用 O(n) 算法来完成。合并 2 个数组没有问题,我可以使用 O(n) 算法轻松完成。我觉得我的教授所要求的对于使用 O(n) 算法的 n 个数组是可撤销的。
我使用下面的算法来拆分数组索引并在每个分区上运行 InsertionSort。我可以将这些开始和结束索引保存到一个二维数组中。我只是不知道如何使用 O(n) 完成合并,因为这将需要多个循环。如果可能的话,有没有人有任何提示。我不是在寻找实际的代码,只是一个关于我应该从哪里开始的提示/
int chunkSize = round(float(arraySize) / numThreads);
for (int i = 0; i < numThreads; i++) {
int start = i * chunkSize;
int end = start + chunkSize - 1;
if (i == numThreads - 1) {
end = arraySize - 1;
}
InsertionSort(&array[start], end - start + 1);
}
Run Code Online (Sandbox Code Playgroud)
编辑:要求是算法是 O(n),其中 n 是数组中的元素数。另外,我需要在不使用最小堆的情况下解决这个问题。
编辑#2:这是我想出的算法。这里的问题是我没有将每次迭代的结果存储回原始数组。我可以将所有内容复制回循环中,但这会很昂贵。除了使用某些东西之外,我还有什么办法可以做到这一点memcpy …
我有一个带静态单元格的UITableViewController.当我有一个单元格的多行时,它们相互重叠.自动布局无法解决问题.以下是故事板和模拟器的屏幕截图.我没有为UITableViewController创建的类的任何代码,所以这不应该是一个问题.
编辑:如果我用空单元格运行模拟器,它显示正常.



swift ×5
ios ×4
arrays ×2
c ×2
objective-c ×2
admob ×1
algorithm ×1
c++ ×1
iad ×1
include ×1
iostream ×1
nsindexpath ×1
sorting ×1
uialertview ×1
uitableview ×1
uitextfield ×1