I have an int named percentComplete (for a SplashScene). I need to make that into text (CCLabelBMFont) that will have the same number as the int, and be updated when the int is. Think of a loading screen that has a number represent how far it is until the game can start (77%). I've started with this so far. I end up with a warning. percentComplete is the int, percentCompleteText is a CCLabelBMFont
-(void)displayPercentCompleteText:(id)sender {
percentCompleteText = (CCLabelBMFont *) [self getChildByTag:kTagPercentComplete];
[percentCompleteText setString:[percentComplete getPercentCompleteAsString]]; //Invalid receiver type 'int'
percentCompleteText.position = ccp(111, 111); //CHANGE POSITION
//SCHEDULE UPDATE EVERY .01s
}
-(NSString *)getPercentCompleteAsString {
return [NSString stringWithFormat:#"%d", percentComplete];
}
I'm assuming -(NSString *)getPercentCompleteAsString is in the same class as -(void)displayPercentCompleteText:(id)sender and percentComplete is a member variable (ivar) of that class ..
Change the line:
[percentCompleteText setString:[percentComplete getPercentCompleteAsString]]
to:
[percentCompleteText setString:[self getPercentCompleteAsString]]
return [NSString stringWithFormat:#"%d", percentComplete];
what if you change the line to
return [NSString stringWithFormat:#"%i", percentComplete];
instead of double..
Related
I'm trying to get the amount of lines in a TextView. I've searched here for a solution, basically every thread has the same answer : contentheight/fontlineheight.
I have a TextView with 8 lines, i run this code and i get contsize : 1.944413
NSLog(#"contsize : %f", descLabel.contentSize.height/descLabel.font.lineHeight);
What am i doing wrong?
For iOS7, a version that take care that you can have differents font (and font size) in your UITextView :
- (NSUInteger)numberOfLinesInTextView:(UITextView *)textView
{
NSLayoutManager *layoutManager = [textView layoutManager];
NSUInteger index, numberOfLines;
NSRange glyphRange = [layoutManager glyphRangeForTextContainer:[textView textContainer]];
NSRange lineRange;
for (numberOfLines = 0, index = glyphRange.location; index < glyphRange.length; numberOfLines++){
(void) [layoutManager lineFragmentRectForGlyphAtIndex:index
effectiveRange:&lineRange];
index = NSMaxRange(lineRange);
}
return numberOfLines;
}
so I have a [NSMutableArray *paths] that contains UIBezierPaths that correspond to each separate path drawn on the screen. I also have another NSMutable array colors that contains the corresponding color for the path. The problem that I am having is that although the two arrays are mutating and I can see the count of both going up, the following drawRect method only seems to draw the last path in the array.
I have scoured through a lot of questions and based on what I have read so far, this very problem seems to get us newbies and mostly because of type mismatch. I even tried to Typecast the return and it was futile.
Here is my code from the drawRect
int cnt = paths.count;
if (cnt != 0)
{
for(int i = 0; i < cnt; i++)
{
NSLog(#"Drawing path:%i", i);
UIColor *color;
color = [colors objectAtIndex:i];
[color setStroke];
UIBezierPath *path = [paths objectAtIndex:i];
path.lineWidth = 2;
[path stroke];
}
}
every time I call [self setNeedsDisplay] it wipes out my entire screen.
Am I correct in assuming that I am not handling my
UIBezierPath *path = [paths objectAtIndex:i];
well?
Thank you very much and in advance.
UPDATE:
I was rummaging through some more codes and now it almost looks like there is another possibility that ARC is releasing the contents of my NSMutable array. I tried the do the following and got a compiler error.
[paths retain];
Besure you add your path to your path array afer each draw
- (void)drawRect:(CGRect)rect {
[brushPattern setStroke];
UIBezierPath *drawPath = [UIBezierPath bezierPath];
drawPath.lineCapStyle = kCGLineCapRound;
drawPath.miterLimit = 0;
drawPath.lineWidth = thisLineWidth;
for (UIBezierPath *path in pathArray)
[drawPath appendPath:path];
UIBezierPath *path = [self pathForCurrentLine];
if (path)
[drawPath appendPath:path];
[drawPath stroke];
}
- (UIBezierPath*)pathForCurrentLine {
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:startPoint];
[path addLineToPoint:endPoint];
return path;
}
I have problems with a simple NSString on Mac OS X:
NSString *letters = #"abcdefghijklmnopqrstuvwxyz0123456789";
myString = [myString stringByAppendingFormat:#"%c", [letters characterAtIndex:3]];
When I try to access this string again, Xcode is returning EXC_BAD_ACCESS
This error just occures, when I'm using the format #"%c"
When I'm using #"%#", sometimes the same error, sometimes this string: control 0x10040a480 and sometimes this:
{(
<CFRunLoopObserver 0x10015ac60 [0x7fff70731ee0]>{locked = No, valid = Yes, activities = 0x21, repeats = Yes, order = 0, callout = _ZL15FlushAllBuffersP19__CFRunLoopObservermPv (0x7fff88a147d4), context = <CFRunLoopObserver context 0x0>}
)}
The errors occur randomly even, if I don't change anything in the code and re-run it.
I try to get a random String by doing:
randomString = #"";
NSString *letters = #"abcdefghijklmnopqrstuvwxyz0123456789";
srand(time(NULL));
for (int i=0; i<5; i++)
{
randomString = [randomString stringByAppendingFormat:#"%c", [letters characterAtIndex:(rand()%[letters length])]];
}
randomString is declared in header.h
I also tried using a NSMutableString but that didn't work too.
Every time I try to access the string (or mutable string) via #"%#" I'm getting EXC_BAD_ACCESS
Any idea?
Hope somebody can help me!
Greets,
Julian
Your problem is that myString gets autoreleased before you access it.
You need to change:
myString = [myString stringByAppendingFormat:#"%c", [letters characterAtIndex:3]];
to:
myString = [[myString stringByAppendingFormat:#"%c", [letters characterAtIndex:3]] retain];
Remember to run [myString release]; when you are done with it.
this works for me:
define letters at top of file:
#define letters #"abcdefghijklmnopqrstuvwxyz0123456789"
then append a random character like this:
NSString *randomString = #"";
for (int i = 0; i < 5; i++)
{
UInt64 index = rand() % [letters length];
NSString *randomCharacter = [letters substringWithRange: NSMakeRange(index, 1)];
randomString = [randomString stringByAppendingString:randomCharacter];
}
NSLog(#"%#", randomString);
I'm making a game where a grid that is x by y square views big. What I'd like to do is be able to create the grid programmatically.
I had something along these lines in mind.
int across = x
int down = y
CGRect dimentions = foo, bar //etc
int distanceBetween = z
///some crazy loop to make it
If possible I'd also like to keep a reference to each, ideally by assigning tags so I can manipulate them later..
Cheers
S
I think this should do the trick
int across = x
int down = y
CGSize dimensions; // make a cgsize here with the correct values
int distanceBetween = z;
NSMutableArray *views = [[NSMutableArray alloc] init];
for (int x = 0; x < across; x++) {
NSMutableArray *downViews = [[NSMutableArray alloc] init];
for (int y = 0; y < down; y++) {
UIView *view; //create view using the dimensions and padding
//any additional setup to the views
[downViews addObject:view];
}
[views addObject:downViews];
[downViews release];
}
Basically it's just a matter of setting the grid sizes and then calculating the x and y positions from there.
It should calculate how many columns across based on the frame size. So that if the frame widens it will show more columns. Not sure if you need it to be resolution independent or not but I've put that in there.
If you need a set number of columns in the grid then just change gridColumns to be a fixed integer.
float gridItemWidth = 225.00;
float gridItemHeight = 225.00;
-(int)gridColumns {
NSRect frame = [self frame];
return (int)frame.size.width / (gridItemWidth);
}
-(int)getxOffset:(int)i {
int xPadding = 10;
return ((i % [self gridColumns]) * (gridItemWidth + xPadding));
}
-(int)getyOffset:(int)i {
int yPadding = 20;
return (floor(i / [self gridColumns]) * (gridItemHeight + yPadding));
}
Then basically in your loop you call getxOffset and getyOffset for the index of your loop to get the position of the grid.
I have manually set the x and y paddings in there to be 10 and 20, but you can set them to whatever you need.
Given a string say "acbXyzKlm" i want to split it to abc, Xyz,Klm. One naive way to do this is to go traverse the string and detect the case change to split. I was wondering if there is a better algorithm for this.
To determine if a point in the string is a valid breaking point, you need to have read both characters around breaking point. So any algorithm to solve this will need to analyze the case of every character.
Your algorithm does just that, hence it's computationally optimal. Any "better" algorithm would be a variant and/or micro-optimization of that, with the same overall complexity.
I needed this today, so I implemented it with a category:
#interface NSString (Extensions)
- (NSString*) spacify;
#end
#implementation NSString (Extensions)
- (NSString*) spacify
{
// ignore irrelevant strings
if (self.length < 1)
return self;
NSMutableString* result = [NSMutableString stringWithString:self];
// create a range starting after the first character
NSRange range;
range.location = 1;
range.length = [self length] - 1;
// match any uppercase character
NSRegularExpression* r = [NSRegularExpression regularExpressionWithPattern: #"[A-Z]"
options: 0
error: nil];
// replace matches with the match preceded by a space
[r replaceMatchesInString: result
options: 0
range: range
withTemplate: #" $0"];
return [NSString stringWithString:result];
}
#end
Tests:
#implementation NSStringExtensionsTest
- (void) testSpacify
{
NSString* target = #"ThisIsAStringX";
NSString* expected = #"This Is A String X";
NSString* actual = [target spacify];
STAssertEqualObjects(expected, actual, nil);
}
- (void) testSpacify_NoMatches_DoesNothing
{
NSString* target = #"thisisstring";
NSString* actual = [target spacify];
STAssertEqualObjects(target, actual, nil);
}
- (void) testSpacify_EmptyString_DoesNothing
{
NSString* target = #"";
NSString* actual = [target spacify];
STAssertEqualObjects(target, actual, nil);
}
- (void) testSpacify_InvalidLength_DoesNothing
{
NSString* target = #"A";
NSString* actual = [target spacify];
STAssertEqualObjects(target, actual, nil);
}
#end