小编Jua*_*lan的帖子

如何计算Swift数组中元素的出现次数?

我已经看到了一些这方面的例子,但所有这些似乎依赖于知道你想要计算哪些元素的出现次数.我的数组是动态生成的,所以我无法知道我想要计算哪个元素的出现次数(我想计算所有这些元素的出现次数).任何人都可以建议吗?

提前致谢

编辑:

也许我应该更清楚,数组将包含多个不同的字符串(例如 ["FOO", "FOO", "BAR", "FOOBAR"]

如何在不知道它们是什么的情况下计算foo,bar和foobar的出现次数?

arrays nsarray ios swift

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

正确管理addObserverForName:object:queue:usingBlock:

我仍然是objective-c中的块的新手,并想知道我的伪代码是否正确.我不确定是否只是删除观察者或者我必须调用removeObserver:name:object:

-(void) scan {
    Scanner *scanner = [[Scanner alloc] init];
    id scanComplete = [[NSNotificationCenter defaultCenter] addObserverForName:@"ScanComplete" 
                        object:scanner 
                        queue:nil 
                        usingBlock:^(NSNotification *notification){
                            /*
                             do something
                             */
                            [[NSNotificationCenter defaultCenter] removeObserver:scanComplete];
                            [scanner release];
                        }];
    [scanner startScan];
}
Run Code Online (Sandbox Code Playgroud)

更新:我EXC_BAD_ACCESS从这个区块收到间歇性,所以这不可能是正确的.

cocoa objective-c ios objective-c-blocks

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

如何检测swift ios xcode中何时更改UIView大小?

我正在使用UIScrollView中的CATiledLayer尝试一些东西.

不知何故,UIScrollView中UIView的大小变为大数.我需要找出导致此调整大小的确切原因.

有没有办法检测UIView的大小(帧,边界)或UIScrollView的contentSize何时调整大小?

我试过了

override var frame: CGRect {
    didSet {
        println("frame changed");
    }
}
Run Code Online (Sandbox Code Playgroud)

在UIView子类中,

但它只在应用程序启动时调用一次,但之后会调整UIView的大小.

cocoa-touch uiscrollview uiview ios swift

20
推荐指数
4
解决办法
3万
查看次数

每次重启Xcode时,Storyboard缺少约束

使用auto layoutin storyboard,我在a上添加一些约束UILabel而没有警告,然后我重新启动Xcode它会向我显示警告,如图片,我修复了警告,但是当我重新启动时Xcode,再次出现相同的警告.现在我的约有20个相同的警告,storyboard每次重新启动时都必须修复它Xcode.

编辑细节

1. A UILabel with width is 0 and constraints is like this
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

2. When I restart Xcode, then it change to :
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

3. And show me the warning:
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

4. Then I have to click the waring button and 'Update Frame' to fix it
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

xcode storyboard ios autolayout nslayoutconstraint

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

UITableView的didSelectRowAtIndexPath方法没有被调用

我有UIViewController一个UITableView在里面.tableview具有自定义单元格,其中包含文本字段.文本字段的代理设置为该类.

在编辑textfield(textFieldDidBeginEditing)时,我有一个下拉(UIView其中有一个tableview).下拉列表以编程方式创建.我面临的问题是,didSelectRowAtIndexPath没有调用dropdown tableview.我正确地将委托,数据源设置为类.我也删除了所有其他手势.我已确保tableview未处于编辑模式.

在下拉列表中单元格的单击,唯一有效的是我的background tableview的textfield的委托方法.

//UITableView class(with textfield from where dropdown is loaded.)
- (void)textFieldDidBeginEditing:(UITextField *)textField{

if(textField.tag == 3){
    if(dropDown == nil) {
        CGFloat f = 200;
        dropDown = [[SFTextFieldDropDown alloc] showDropDownWithSender:textField withHeight:f andElements:self.list];
        dropDown.delegate = self;
        }
    }
}

// Custom Drop down class.
- (id)showDropDownWithSender:(UITextField *)senderTextField withHeight:(CGFloat)height andElements:(NSArray *)array{

    table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, btn.size.width, 0)];
    table.delegate = self;
    table.dataSource = self;
    table.layer.cornerRadius = 5;
    table.backgroundColor = …
Run Code Online (Sandbox Code Playgroud)

objective-c uitableview ios

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

使用swift在设定的时间每天重复本地通知

我是iOS开发的新手,但已经创建了应用程序,我正在尝试创建一段时间的每日通知.目前,通知对于给定的日期/时间执行一次.我不确定如何使用repeatInterval方法每天安排它.每天重复通知的最佳方法是什么?任何帮助将不胜感激(Y).

    var dateComp:NSDateComponents = NSDateComponents()
    dateComp.year = 2015;
    dateComp.month = 06;
    dateComp.day = 03;
    dateComp.hour = 12;
    dateComp.minute = 55;
    dateComp.timeZone = NSTimeZone.systemTimeZone()

    var calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    var date:NSDate = calender.dateFromComponents(dateComp)!

    var notification:UILocalNotification = UILocalNotification()
    notification.category = "Daily Quote"
    notification.alertBody = quoteBook.randomQuote()
    notification.fireDate = date
    notification.repeatInterval = 

    UIApplication.sharedApplication().scheduleLocalNotification(notification)
Run Code Online (Sandbox Code Playgroud)

cocoa-touch ios uilocalnotification swift

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

Python:一个月的第三个星期五

我是一个新手python程序员,我需要编写一个脚本来检查给定日期(以"月,日年"形式的字符串传递)是否是该月的第三个星期五.我使用的是Python 2.7.

例如,这些日期可以帮助您更好地理解我的问题.手头有年历.

  • 输入--->输出
  • '2013年1月18日'--->真的
  • '2013年2月22日'--->错误
  • '2013年6月21日'--->真的
  • '2013年9月20日'--->真的

我只想使用该语言提供的标准类,如时间,日期时间,日历等.

我已经查看了一些答案,但我看到每天的addidng /减少86400秒的计算,甚至根据一个月的天数进行比较.恕我直言这些是错误的,因为Python中的libreries已经处理了这些细节,所以没有必要重新发明轮子.日历和日期也很复杂:闰年,闰秒,时区,周数等等.所以我认为最好让图书馆解决这些棘手的细节.

在此先感谢您的帮助.

python datetime calendar date datetimeoffset

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

屏幕锁定时,HealthStore enableBackgroundDelivery

您好我正在尝试设置启用了后台传递的运行状况存储观察器.我的问题是,当屏幕被锁定时,它不会传递任何东西.我已经简化了我的代码来解决这个问题:)我在我的plist中有HealthKit并且我接受了healthStore类型的步数.当应用程序打开并且屏幕未锁定时,一切都很好.但是当屏幕被锁定时,我没有得到任何观察.出于测试目的,频率设置为立即.

我的代码如下

- (void)setupHealthStore{
if ([HKHealthStore isHealthDataAvailable])
{
    NSSet *readDataTypes = [self dataTypesToRead];
    self.healthStore = [[HKHealthStore alloc]init];
    [self.healthStore requestAuthorizationToShareTypes:nil readTypes:readDataTypes completion:^(BOOL success, NSError *error)
     {
         if (success)
         {
             HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
             [self.healthStore enableBackgroundDeliveryForType:quantityType frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError *error)
             {
                 if (success)
                 {
                     [self setupObserver];
                 }
             }];
         }
     }];
}
Run Code Online (Sandbox Code Playgroud)

}

在AppDelegate didfinishLaunchWithOptions中调用上述方法

下一个方法是

- (void)setupObserver{
HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
HKObserverQuery *query = [[HKObserverQuery alloc]initWithSampleType:quantityType predicate:nil updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error)
{
    if (!error)
    {
        [self alarm]; …
Run Code Online (Sandbox Code Playgroud)

objective-c background-process ios healthkit hkhealthstore

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

数组初始化程序简写与Swift中的可选值

我试图使用repeatedValues初始化程序初始化包含可选值的数组,我惊讶地发现以下代码无法编译

let a: Int?[] = Int?[](count: 10, repeatedValue:nil)
// error - Value of Int?[]? not unwrapped; did you mean to use '!' or '?'?
Run Code Online (Sandbox Code Playgroud)

有趣的是类型签名Int?[]?,例如可选Array的可选项Int.这感觉就像一个错误,但也许我对语法缺少了一些东西.我已经查看了一些语言参考但尚未找到答案.

更明确的Array<Int?>类型初始化程序按预期工作

let b: Int?[] = Array<Int?>(count: 10, repeatedValue:nil)
// compiles fine
Run Code Online (Sandbox Code Playgroud)

有没有其他人遇到这个并且可以解决一些问题?

编辑

结合非可选类型的额外工作示例来突出显示故障

let c: Int[] = Int[](count: 10, repeatedValue:0)
// non-optional shorthand works fine

class D { var foo = 1 }
let d: D[] = D[](count:10, repeatedValue:D())
// custom class works fine using the …
Run Code Online (Sandbox Code Playgroud)

arrays optional swift

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

快速改变uiviewcontroller的观点

我创建了一个新的UIViewController,我不使用故事板,这是我的代码.我想改变我的视图框架,这对我不起作用,我试图添加viewWillAppear,它仍然不起作用,我知道我可以添加一个新UIView的来做它.我可以更改viewcontroller的视图吗?谢谢你的帮助.

import UIKit

class NewDetailViewController: UIViewController {

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

    }
    override func viewDidLoad() {
        super.viewDidLoad()

        let statusBarHeight: CGFloat = UIApplication.sharedApplication().statusBarFrame.height
        let navBarHeight             = self.navigationController?.navigationBar.frame.size.height

        println("statusBarHeight: \(statusBarHeight) navBarHeight: \(navBarHeight)")

        // view
        var x:CGFloat      = self.view.bounds.origin.x
        var y:CGFloat      = self.view.bounds.origin.y + statusBarHeight + CGFloat(navBarHeight!)
        var width:CGFloat  = self.view.bounds.width
        var height:CGFloat = self.view.bounds.height - statusBarHeight - CGFloat(navBarHeight!)
        var frame:CGRect   = CGRect(x: x, y: y, width: width, height: 100)

        println("x: \(x) y: \(y) width: …
Run Code Online (Sandbox Code Playgroud)

uiviewcontroller ios swift

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