我想制作像'乒乓球'这样的小游戏.一切都很好,但是现在,当我想添加一个分数时,游戏会冻结.我将此代码用于我的另一个项目,一切都很好.
以下是该score部分的代码:
-(void)scoreCount{
score ++;
if(scoreLabel == nil){
scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"ROTORcapExtendedBold"];
scoreLabel.fontSize = 40;
scoreLabel.position = CGPointMake(self.frame.size.width/2,self.frame.size.height/3);
scoreLabel.zPosition = 0;
}
[self addChild:scoreLabel];
scoreLabel.text = [NSString stringWithFormat:@"%ld",(long)score];
}
Run Code Online (Sandbox Code Playgroud)
在控制台我收到此消息:
终止应用程序由于未捕获的异常 'NSInvalidArgumentException',理由是: 'Attemped添加已经具有父SKNode:名称:'(null)的文字: '1' 的fontName: 'ROTORcapExtendedBold' 位置:{189.33333,106.66666}"
删除该行后scoreLabel == nil,应用程序不会冻结,但屏幕上的分数会复制旧分数并使分数无法读取.
我该如何解决?
我是初学者,也许这是一个微不足道的问题.我有这个方法:
-(NSString *)getInfoFormediaItem:(MPMediaItemCollection *)list {
NSString *trackCount;
if ([list count] > 0) {
trackCount = [NSString stringWithFormat:NSLocalizedString(@"%lu Songs", @""), (unsigned long)[list count]];
} else if([list count] == 1) {
trackCount = [NSString stringWithFormat:NSLocalizedString(@"1 Song", @"")];
} else {
trackCount = [NSString stringWithFormat:NSLocalizedString(@"0 Song", @"") ];
}
return [NSString stringWithFormat:@"%@", trackCount];
}
Run Code Online (Sandbox Code Playgroud)
我想在这里用MPMediaItemCollection调用它:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if( cell == nil )
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
MPMediaQuery …Run Code Online (Sandbox Code Playgroud) 我有这个扩展名:
extension String {
public func seperateHashtags(char : String) -> [String]{
var word : String = ""
var words : [String] = [String]()
for chararacter in self.characters {
if String(chararacter) == char && word != "" {
words.append(word)
word = char
}else {
word += String(chararacter)
}
}
words.append(word)
return words
}
}
Run Code Online (Sandbox Code Playgroud)
我想从任何给定的文本中获取一组主题标签.我有一个UITextView包含文字:
write anything here... #one #two three #four
Run Code Online (Sandbox Code Playgroud)
我希望有一个输出:
["one", "two", "four"]
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时:
print(myTextView.text.seperateHashtags("#"))
Run Code Online (Sandbox Code Playgroud)
我越来越:
["write anything here... ", "#one ", "#two three ", "#four"] …Run Code Online (Sandbox Code Playgroud) 无法在 NSView ( MacOs ) 中绘制右对齐的字符串\n这是我的代码,它崩溃了:
\n-[__SwiftValue lineBreakMode]:无法识别的选择器发送到实例 0x600003408750
\nlet attrs = [NSAttributedString.Key.foregroundColor:NSColor.white,\n NSAttributedString.Key.paragraphStyle:NSTextAlignment.right\n ] as [NSAttributedString.Key : Any]\n\n let absolutPoint = NSPoint(x: 0.0, y: 0.0)\n var convertedPt = convertPoint(dataPoint:se,scaleX:scaleX,scaleY:scaleY)\n convertedPt.x = absolutPoint.x\n \n let scalaLabel = NSString(format:"%3.0f",se.y)\n\n let place = NSMakeRect(convertedPt.x, convertedPt.y, 80.0, 12.0)\n scalaLabel.draw(in:place ,withAttributes:attrs )\nRun Code Online (Sandbox Code Playgroud)\n如果我没有在attrs中设置paragraphStyle,则文本会正确绘制。但不是右对齐的。\n知道我的代码有什么\xc2\xb4s 问题吗?
\n我有两个NSString,A和B.
我希望A成为B \n.
我能怎么做?
如果在我使用的方法中
NSString *string_B = [[NSString alloc] initWithString:@"something_from_a_DB"];
NSString *string_A = [[NSString alloc] initWithString:@"something_from_a_DB"];
if (aTrueCondition) {
string_C = [NSString stringWithFormat:@"%@\n%@", string_B, string_A];
} else {
string_C = string_A;
}
Run Code Online (Sandbox Code Playgroud)
是string_C = string_A; 内存泄漏还是好的?
我添加了[string_A release],因为string_C是一个保留属性.现在它有效.
我的代码工作得很好,直到知道,如果我在文本字段中输入一个双位数字(如12),nslog返回2个单位数字(如1和2).现在我需要将这2个单位数字放入2个字符串中.有人可以帮助我吗 提前致谢.
NSString *depositOverTotalRwy = [NSString stringWithFormat:@"%@", [deposit text]];
NSArray *components = [depositOverTotalRwy
componentsSeparatedByString:@"/"];
NSString *firstThird = [components objectAtIndex:0];
for(int i = 0; i < [firstThird length]; i++)
{
char extractedChar = [firstThird characterAtIndex:i];
NSLog(@"%c", extractedChar);
}
Run Code Online (Sandbox Code Playgroud) 嗨,我正在开发一个代码,我需要从uitextview执行删除最后一个字符,我试过这样
-(IBAction)delete:(id)sender{
uitextview.text =[NSString stringWithFormat:@"%@%@",uitextview.text,@"\b"];
}
Run Code Online (Sandbox Code Playgroud)
在删除按钮操作我给了这个代码,但这不执行删除.请帮助完成这项工作,并提前感谢
我已经看到了我的任务的一个答案,但我现在找不到它.我想检测一个字符串是否有空字并且至少包含""或""(两个空格"或多个空格,或者不是.如果没有,我会将其添加到Nsmutablearray.如果是空的或至少一个空格,我希望不要写入mutablearray.
怎么解决这个?
编辑2011年10月12日:
伙计们,谢谢.
我很抱歉,我不清楚我的愿望.我想检查字符串是否为空或包含没有任何字符的空格.我在下面发布了我的答案.
我有一个NSString看起来像/test/test1/test2.我怎么能删除它test2,知道它可以以任何其他方式命名?谢谢!
nsstring ×10
objective-c ×7
ios ×3
cocoa ×2
iphone ×2
swift ×2
cocoa-touch ×1
draw ×1
macos ×1
sklabelnode ×1
sprite-kit ×1
string ×1
utf-8 ×1
xcode ×1