小编ric*_*ter的帖子

带有索引的UITableViewController和UILocalizedIndexedCollat​​ion的NSFetchedResultsController

我有一个使用NSFetchedResultsController的表.这会给我一个带有标题的索引

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[_fetchedResultsController sections] count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[[_fetchedResultsController sections] objectAtIndex:section] name];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger numberOfRows = 0;
    NSArray *sections = _fetchedResultsController.sections;
    if(sections.count > 0)
    {
        id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:section];
        numberOfRows = [sectionInfo numberOfObjects];
    }
    return numberOfRows;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [_fetchedResultsController sectionIndexTitles];
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return [_fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
}
Run Code Online (Sandbox Code Playgroud)

但我想使用UILocalizedIndexCollat​​ion来获得完整的字母表.

如何将这些方法连接到NSFetchedResultsController?

我想我需要从当前的Collat​​ion中获取索引标题

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView …
Run Code Online (Sandbox Code Playgroud)

objective-c nsfetchedresultscontroller uilocalizedcollation

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

generic [T.Generator.Element]初始化程序

我正在努力学习Swift和研究Ch中的例子.苹果公司的一本书.最后一个练习给我带来了很多麻烦,我正在尝试构造一个函数,它返回作为参数传递的两个序列的公共元素.这是我试过的代码:

func anyCommonElements <T, U where T: SequenceType, U: SequenceType, T.Generator.Element: 
Equatable, T.Generator.Element == U.Generator.Element>
    (lhs: T, rhs: U) -> [T.Generator.Element] {
        var result : [T.Generator.Element] // how to default-initialize it?
        for lhsItem in lhs {
            for rhsItem in rhs {
                if lhsItem == rhsItem {
                    result.append(lhsItem)
                }
            }
        }
        return result
}
Run Code Online (Sandbox Code Playgroud)

我唯一的问题是我不知道如何初始化result(类型[T.Generator.Element]),因此我不能使用它来将公共元素附加到其中.我试过了明显的事

var result : [T.Generator.Element] = [T.Generator.Element]()
Run Code Online (Sandbox Code Playgroud)

但是我得到了错误Could not find member Element,如果我尝试

var result : [T.Generator.Element] = [T.Generator.Element()]()
Run Code Online (Sandbox Code Playgroud)

编译器吐出 …

swift

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

随机运动/湍流 - SWIFT

我正在 Iphone 和 Ipad 上开发游戏,就像太空入侵者一样。

要摧毁的气球从屏幕顶部直线落下。

这是我添加它们的代码:

func addBalloonv(){


    var balloonv:SKSpriteNode = SKSpriteNode (imageNamed: "ballonvert.png")


    balloonv.physicsBody = SKPhysicsBody (circleOfRadius: balloonv.size.width/2)
    balloonv.physicsBody.dynamic = true
    balloonv.physicsBody.categoryBitMask = balloonCategory | greenCategory
    balloonv.physicsBody.contactTestBitMask = flechetteCategory
    balloonv.physicsBody.collisionBitMask = balloonCategory
    balloonv.physicsBody.mass = 1
    balloonv.physicsBody.restitution = 1
    balloonv.physicsBody.allowsRotation = true


    let minX = balloonv.size.width/2
    let maxX = self.frame.size.width - balloonv.size.width/2
    let rangeX = maxX - minX
    let position:CGFloat = CGFloat(arc4random()) % CGFloat(rangeX) + CGFloat(minX)

    balloonv.position = CGPointMake(position, self.frame.size.height+balloonv.size.height)

    self.addChild(balloonv)
Run Code Online (Sandbox Code Playgroud)

我有一个气球颜色的功能。

所以目前它们直线移动,我正在寻找从顶部和两侧的随机运动(像空气中的气球一样的湍流)。

我怎样才能做到这一点?

非常感谢 !!

random ios sprite-kit swift ios8

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

CAShapeLayer 未在动画中更新

我有一个包含基本形状的 CAShapeLayer,它是在“drawRect”方法期间创建的,如下所示。

override func drawRect(frame: CGRect)
    {
        let rectangleRect = CGRectMake(frame.minX + 2.5, frame.minY + 2.5, frame.width - 5, frame.height - 5)
        let rectanglePath = UIBezierPath(roundedRect: rectangleRect, cornerRadius: 4)
        shapeLayer.path = rectanglePath.CGPath

        if (self.active == true)
        {
            shapeLayer.fillColor = selectedColor.CGColor
        } else
        {
            shapeLayer.fillColor = buttonColor.CGColor
        }

        shapeLayer.strokeColor = buttonOutlineColor.CGColor
        shapeLayer.lineWidth = 5.0

        self.layer.addSublayer(shapeLayer)

    }
Run Code Online (Sandbox Code Playgroud)

我用它来创建一个带有笔划的简单正方形。然后我有另一种方法,它有一个动画CAShapeLayer。但是,当调用该方法时,.NET 上没有任何更改CAShapeLayer。动画方法如下所示。

let animation = CABasicAnimation(keyPath: "strokeColor")
    animation.duration = 0.5
    animation.repeatCount = 10
    animation.fromValue = UIColor.whiteColor()
    animation.toValue = UIColor.redColor()
    animation.autoreverses = …
Run Code Online (Sandbox Code Playgroud)

core-animation ios swift

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

Swift:使控制按钮不随相机移动

我正在建立一个平台游戏,当他走路时我让相机跟随玩家:

let cam = SKCameraNode()

override func didMoveToView(view: SKView) {
    self.camera = cam
    ...
}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */

    cam.position = Player.player.position
    ...
Run Code Online (Sandbox Code Playgroud)

但是,当相机移动时,控制按钮也会移动 在此输入图像描述

我该怎么办才能使控制按钮保持静止?

ios swift

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

在 NSHTTPURLResponse 处下载 JSON 数据失败

我正在尝试从 URL 获取 JSON 文件并使用 Swift 返回内容。但是,代码let httpResponse = response as! NSHTTPURLResponse 在以下代码中的行失败。我在这一行遇到异常,Xcode 进入调试模式。

class func downloadJSONFile()->AnyObject
    {
        let requestURL: NSURL = NSURL(string: "http://www.learnswiftonline.com/Samples/subway.json")!
        let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL)
        let session = NSURLSession.sharedSession()
        var json:AnyObject = ""
        let task = session.dataTaskWithRequest(urlRequest) {
            (data, response, error) -> Void in

            let httpResponse = response as! NSHTTPURLResponse
            let statusCode = httpResponse.statusCode

            if (statusCode == 200) {

                do{
                    json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments)

                }catch {
                    print("Error with Json: \(error)")

                }

            }

        } …
Run Code Online (Sandbox Code Playgroud)

nsjsonserialization nsurlsession swift

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

检查字符串是否有空格

我有一个包含 url 的字符串。我正在尝试检查网址是否包含无效空格。

let url = "http://www.example.com/images/pretty pic.png"
Run Code Online (Sandbox Code Playgroud)

正如您在本例中看到的,pretty 和 pic 之间有一个空格。谢谢。

iphone swift

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

类方法swift中的实例方法

我有一个类TestClass和一个类和实例方法

class  TestClass {

    class func classMethod(){

       print("how do i call instance method from here")

    }

    func instanceMethod(){

        print("call this instance method")

    }


}
Run Code Online (Sandbox Code Playgroud)

我的问题是我如何调用instanceMethodclassMethod

我注意到的一种方式是

class func classMethod(){

   TestClass().instanceMethod()

}
Run Code Online (Sandbox Code Playgroud)

但是,这是一个好方法吗?

function ios swift

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

art.scnassets代表什么?

我正在研究ARKit(iPhone框架),其中一个免费提供ARKit项目的文件夹是一个标题为的文件夹:art.scnassets.这些代表什么:art..scn.deatex

augmented-reality ios scenekit arkit

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

iOS swift测试1.2版错误,

原始代码

  // add a tap gesture recognizer
  let tapGesture = UITapGestureRecognizer(target: self, action: "handleTap:")
  let gestureRecognizers = NSMutableArray()
  gestureRecognizers.addObject(tapGesture)
  if let existingGestureRecognizers = sceneView.gestureRecognizers {
    gestureRecognizers.addObjectsFromArray(existingGestureRecognizers)
  }
  sceneView.gestureRecognizers = gestureRecognizers

error -->> sceneView.gestureRecognizers = gestureRecognizers
Run Code Online (Sandbox Code Playgroud)

很遗憾地更新到swift 1.2的最新版本

'NSMutableArray' is not convertible to '[AnyObject]'; did you mean to use 'as!' to force downcast?
Run Code Online (Sandbox Code Playgroud)

它想要改变行

sceneView.gestureRecognizers = gestureRecognizers as! [AnyObject]
Run Code Online (Sandbox Code Playgroud)

在更改接受后,我现在收到此错误

Cannot assign a value of type '[AnyObject]' to a value of type '[AnyObject]?'
Run Code Online (Sandbox Code Playgroud)

迅速的迁移者没有帮助,发行说明也没有.

这不是我的代码,因为我不想搞砸我自己的回购,所以这是一个来自github的项目.

https://github.com/ryanshelby/Stonehenge

谢谢你的帮助

ios swift

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

: 和 = 之间的区别

我用 Swift 编程已经有一段时间了。但是我仍然没有掌握全部基础知识,也没有理解“=”和“:”之间的区别。我们使用这些来声明变量。但是在 Swift 中编程时等于和冒号有什么区别?任何帮助将不胜感激!

variables var swift xcode7

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