小编67c*_*ies的帖子

为什么函数调用需要Swift中的参数名?

我在一个类中有这个函数:

func multiply(factor1:Int, factor2:Int) -> Int{
    return factor1 * factor2
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用这个来调用函数:

var multResult = calculator.multiply(9834, 2321)
Run Code Online (Sandbox Code Playgroud)

问题是编译器希望它看起来更像这样:

var multResult = calculator.multiply(9834, factor2: 2321)
Run Code Online (Sandbox Code Playgroud)

为什么第一个会导致错误?

methods named-parameters swift

65
推荐指数
1
解决办法
3万
查看次数

Swift将UInt转换为Int

我有这个表达式返回一个UInt32:

let randomLetterNumber = arc4random()%26
Run Code Online (Sandbox Code Playgroud)

我希望能够使用此if语句中的数字:

if letters.count > randomLetterNumber{
    var randomLetter = letters[randomLetterNumber]
}
Run Code Online (Sandbox Code Playgroud)

这个问题是控制台给了我这个

Playground execution failed: error: <REPL>:11:18: error: could not find an overload for '>' that accepts the supplied arguments
if letters.count > randomLetterNumber{
   ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

问题是UInt32无法与之相提并论Int.我想投randomLetterNumber一个Int.我试过了:

let randomLetterUNumber : Int = arc4random()%26
let randomLetterUNumber = arc4random()%26 as Int
Run Code Online (Sandbox Code Playgroud)

这两个原因 could not find an overload for '%' that accepts the supplied arguments.

如何转换值或在if语句中使用它?

integer uint32 swift

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

Swift中的强引用和弱引用

在Objective C中,您可以将属性定义为具有强引用或弱引用,如下所示:

@property(strong)...
@property(weak)...
Run Code Online (Sandbox Code Playgroud)

这怎么做得很快?

reference weak-references objective-c swift

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

如何本地化后退按钮?

我正在将应用程序翻译成印度尼西亚语.故事板使用导航控制器并为其所有视图推送segue.当我从主菜单进入视图时,后退按钮被正确翻译,但是当我从那里进入视图时(远离主菜单的两个视图),后退按钮显示"返回".在此先感谢您的帮助.

localization back-button uinavigationcontroller ios

19
推荐指数
2
解决办法
8620
查看次数

Xcode错误检索开发者信息?

我正在关注这个苹果文档并试图为新的apple-id添加配置文件和签名身份.问题是,当我在Xcode中添加一个新的apple-id - > Settings - > Accounts时,我收到"Error Fetching Developer Info". 获取开发人员信息时出错

是我在谷歌上搜索的所有内容.我多次重启Xcode无济于事.有没有人遇到过这个错误?

我该如何解决这个问题?

ide xcode ios provisioning-profile apple-id

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

正则表达式通过javascript从字符串中删除重复的字符

我找到了一种使用正则表达式从字符串中删除重复字符的方法.

function RemoveDuplicates() {
    var str = "aaabbbccc";
    var filtered = str.replace(/[^\w\s]|(.)\1/gi, "");  
    alert(filtered);
}
Run Code Online (Sandbox Code Playgroud)

输出:abc 这个工作正常.

但如果str = "aaabbbccccabbbbcccccc"那时输出是abcabc.有没有办法只获取唯一的字符或删除所有重复的字符?如果有任何办法,请告诉我.

javascript regex

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

用CALayer画一条线

我正在尝试使用CALayer在两点之间划一条线.这是我的代码:

//positions a CALayer to be a line between a parent node and its subnodes.

-(void)makeLineLayer:(CALayer *)layer lineFromPointA:(CGPoint)pointA toPointB:(CGPoint)pointB{
    NSLog([NSString stringWithFormat:@"Coordinates: \n Ax: %f Ay: %f Bx: %f By: %f", pointA.x,pointA.y,pointB.x,pointB.y]);

    //find the length of the line:
    CGFloat length = sqrt((pointA.x - pointB.x) * (pointA.x - pointB.x) + (pointA.y -     pointB.y) * (pointA.y - pointB.y));
    layer.frame = CGRectMake(0, 0, 1, length);

    //calculate and set the layer's center:
    CGPoint center = CGPointMake((pointA.x+pointB.x)/2, (pointA.y+pointB.y)/2);
    layer.position = center;

    //calculate the angle of the …
Run Code Online (Sandbox Code Playgroud)

cocoa-touch transformation objective-c calayer quartz-core

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

触摸期间未检测到自定义SKSpriteNode

在学习SpriteKit时,我正在尝试制作一款小型冒险游戏.我正在创建一个英雄,并将其添加到场景中,然后,在此期间touchesBegan:,我检测到触摸是否源自英雄,并相应地采取行动.

如果我添加英雄作为SKSpriteNode触摸检测他.如果我把他添加为SKSpriteNode触摸的子类不!添加的区别:

_heroNode = [SKSpriteNode spriteNodeWithImageNamed:@"hero.png"];
Run Code Online (Sandbox Code Playgroud)

VS

_heroNode = [[ADVHeroNode alloc] init];
Run Code Online (Sandbox Code Playgroud)

init看起来像这样:

- (id) init {
    if (self = [super init]) {

        self.name = @"heroNode";
        SKSpriteNode *image = [SKSpriteNode spriteNodeWithImageNamed:@"hero.png"];
        [self addChild:image];
    }
    return self;
}
Run Code Online (Sandbox Code Playgroud)

将英雄添加为SKSpriteNode的子类可以将其添加到场景中,但触摸不会检测到他.我touchesBegan:看起来像这样:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];
    if (YES) NSLog(@"Node name where touch began: …
Run Code Online (Sandbox Code Playgroud)

ios sprite-kit

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

UIScrollView不滚动?

我有一个UIScrollView在Interface Builder创建了一堆UITextViewUIImageView对象已经添加.我把它连接到一个IBOutlet,并在-(void)viewDidLoad我设置其内容大小1200x2000.用户交互已启用,多点触控,滚动已启用并显示垂直和水平滚动指示器均已设置为YES.当我在iOS模拟器中启动应用程序时,它不会滚动.可能是这种行为的原因是什么?

iphone cocoa-touch objective-c uiscrollview ios

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

Swift和Spritekit不会在运行iOS 7.1的设备上运行

当我尝试使用Xcode 6在我的设备上运行我的Swift项目时,我收到了一个非常奇怪的警告.

该设备运行iOS 7.1,我的Mac正在运行Mavericks.

我在Swift和SpriteKit中编写了一个小游戏,它可以在模拟器中运行,但是当我尝试在我的设备上运行它时,我收到警告,而我的设备只显示黑色背景.

尝试运行一个新的SpriteKit项目时,我得到了相同的结果.但是,如果我使用Swift运行一个新的单页应用程序,它会正常运行.

这是我收到的警告:

在此输入图像描述

有什么建议?

sprite-kit swift xcode6

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