我在一个类中有这个函数:
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)
为什么第一个会导致错误?
我有这个表达式返回一个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语句中使用它?
在Objective C中,您可以将属性定义为具有强引用或弱引用,如下所示:
@property(strong)...
@property(weak)...
Run Code Online (Sandbox Code Playgroud)
这怎么做得很快?
我正在将应用程序翻译成印度尼西亚语.故事板使用导航控制器并为其所有视图推送segue.当我从主菜单进入视图时,后退按钮被正确翻译,但是当我从那里进入视图时(远离主菜单的两个视图),后退按钮显示"返回".在此先感谢您的帮助.
我找到了一种使用正则表达式从字符串中删除重复字符的方法.
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
.有没有办法只获取唯一的字符或删除所有重复的字符?如果有任何办法,请告诉我.
我正在尝试使用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) 在学习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) 我有一个UIScrollView
在Interface Builder创建了一堆UITextView
和UIImageView
对象已经添加.我把它连接到一个IBOutlet
,并在-(void)viewDidLoad
我设置其内容大小1200x2000
.用户交互已启用,多点触控,滚动已启用并显示垂直和水平滚动指示器均已设置为YES
.当我在iOS模拟器中启动应用程序时,它不会滚动.可能是这种行为的原因是什么?
当我尝试使用Xcode 6在我的设备上运行我的Swift项目时,我收到了一个非常奇怪的警告.
该设备运行iOS 7.1,我的Mac正在运行Mavericks.
我在Swift和SpriteKit中编写了一个小游戏,它可以在模拟器中运行,但是当我尝试在我的设备上运行它时,我收到警告,而我的设备只显示黑色背景.
尝试运行一个新的SpriteKit项目时,我得到了相同的结果.但是,如果我使用Swift运行一个新的单页应用程序,它会正常运行.
这是我收到的警告:
有什么建议?
ios ×4
swift ×4
objective-c ×3
cocoa-touch ×2
sprite-kit ×2
apple-id ×1
back-button ×1
calayer ×1
ide ×1
integer ×1
iphone ×1
javascript ×1
localization ×1
methods ×1
quartz-core ×1
reference ×1
regex ×1
uint32 ×1
uiscrollview ×1
xcode ×1
xcode6 ×1