Load all the Contacts from iphone PhoneBook in a variable - iphone-sdk-3.0

I am naive to the xcode programming. I need to load all the contacts from the iPhone PhoneBook on a variable.
Could you please suggest me some library or something like that. Any sample code would be highly appreciable.
Looking forward to a response.:)
Thanks in advance!!..... :)

Hey all after a long fight I found the following code working
self.dataSource = [[NSMutableArray alloc]init]; // dataSouce is delared in .h file
ABAddressBookRef addressBook = ABAddressBookCreate();
NSMutableArray *allPeople = (NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
int nPeople = ABAddressBookGetPersonCount(addressBook);
for(int i=0; i < nPeople; i++ ){
ABRecordRef person = [allPeople objectAtIndex:i];
NSString *name = #"";
if(ABRecordCopyValue(person, kABPersonFirstNameProperty) != NULL)
name = [[NSString stringWithFormat:#"%#", ABRecordCopyValue(person, kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
[dataSource addObject: name];
}

Related

extracting a selection of Textures from a Texture Atlas

I'm trying to write a method to do the above. My Texture Atlas contains more than 500 Textures. When i store my extracted Textures in a new Array and log its description the Array contains my correct named 3 Textures PLUS all of the Textures the original Atlas contained. Everyone of these is referred to " 'MissingResource.png' (128 x 128)".
My Question is: How can i ONLY store the 3 textures i want in the new Array? Here is the code:
-(void)createSelectedTexturesWith:(NSString*)ImageName;
{
int bgCount = 1;
NSMutableString *tempstring = [NSMutableString stringWithString:ImageName];
SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:#"textures"];
NSMutableArray *Leveltextures = [[NSMutableArray alloc]init];
NSUInteger numImages = atlas.textureNames.count;
for (int i=1;i <= numImages; i++) {
[tempstring appendFormat:#"_%02d.png",bgCount];
NSString *textureName = [NSString stringWithString:tempstring];
SKTexture *temp = [atlas textureNamed:textureName];
[Leveltextures addObject:temp];
[tempstring setString:ImageName];
bgCount++;
}
NSLog(#"Leveltextures Content: %#",[Leveltextures description]);
}
Not sure I understand what you want. But if I do understand it correctly, why can't you just check if the textureName's prefix matches your imageName and only create the texture and add to the array in that case only ?
Thank you prototypical, that did it for me. Though the code is maybe not too elegant, here it is. Maybe it helps somebody else.
-(void)createSelectedTexturesWith:(NSString*)ImageName;
{
bgCount=1;
NSMutableString *tempstring = [NSMutableString stringWithString:ImageName];
SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:#"textures"];
NSMutableArray *Leveltextures = [[NSMutableArray alloc]init];
NSArray *atlasArray = [atlas textureNames];
for (NSString *string in atlasArray) {
if ([string hasPrefix:ImageName]){
[tempstring appendFormat:#"%02d.png",bgCount];
NSString *textureName = [NSString stringWithString:tempstring];
SKTexture *temp = [atlas textureNamed:textureName];
[Leveltextures addObject:temp];
[tempstring setString:ImageName];
bgCount++;
}
}
NSLog(#"Leveltextures Content: %#",[Leveltextures description]);
}

How to view the entire content of an array in a label (xcode 4.1)

i'm programming in Obj-c with xcode4.1, i have an array with numbers in it, and i want to visualize all of them in a label...can anyone help me around this please?
thanks!
this is the code:
combinedString=[[NSMutableArray alloc] init];
NSString *finalStringLabel=#"";
for (i=0; i<=textLength; i++) {
//character coding
char myChar = [myString characterAtIndex:i];
NSString *myCharS=[NSString stringWithFormat:#"%c", myChar];
int asciiCode=[myCharS characterAtIndex:0];
NSString *asciiS=[NSString stringWithFormat:#"%i", asciiCode];
[combinedString addObject:asciiS];
}
finalStringLabel=[NSString stringWithFormat:#"", [combinedString componentsJoinedByString:#"."]];
myLabel.text=finalStringLabel;
[combinedString release];
}
You can use this
NSArray *yourArray;
NSString *createdString = [yourArray componentsJoinedByString:#" "];
myLabel.text = createdString;
As your array is combinedString,
combinedString=[[NSMutableArray alloc] init];
looks like you are providing values after this line or this is not a property (this is a local as you are releasing it later), and your code in not complete.
Anyways,
You don't need to create an empty string and then assign new object to it, need to do as :
myLabel.text=[combinedString componentsJoinedByString:#"."];
[combinedString release];
}

replace an object inside nsmutablearray

I have a NSMutableArray where i want to replace the sign | into a ; how can i do that?
NSMutableArray *paths = [dic valueForKey:#"PATH"];
NSLog(#"pathArr ", paths)
pathArr (
(
"29858,39812;29858,39812;29925,39804;29936,39803;29949,39802;29961,39801;30146,39782;30173,39779;30220,39774;30222,39774|30215,39775;30173,39779;30146,39782;29961,39801;29949,39802;29936,39803;29925,39804;29858,39812;29858,39812;29856,39812;29800,39819;29668,39843;29650,39847;29613,39855;29613,39855;29613,39856;29605,39857;29603,39867;29603,39867;29599,39892;29596,39909;29587,39957;29571,40018;29563,40038;29560,40043"
)
)
Update
This is where i got my path from
NSArray *BusRoute = alightDesc;
int i;
int count = [BusRoute count];
for (i = 0; i < count; i++)
{
NSLog (#"BusRoute = %#", [BusRoute objectAtIndex: i]);
NSDictionary *dic = [BusRoute objectAtIndex: i];
NSMutableArray *paths = [dic valueForKey:#"PATH"];
}
Provide that your object in the array path is string, you can do this
NSMutableArray *path2=[[NSMutableArray alloc]initWithArray:nil];
for (NSObject *obect in path) {
for (NSString *string in (NSArray*)obect) {
[path2 addObject:[string stringByReplacingOccurrencesOfString:#"|" withString:#","]];
}
}
NSLog(#"pathArr %# ", path2);
your array paths contains an another array which has string as object.
Hope this helps
//Copy the Array into a String
NSString *str = [paths componentsJoinedByString: #""];
//then replace the "|"
str = [str stringByReplacingOccurrencesOfString:#"|" withString:#";"];
i did this to replace a string in a .plist so it might work for you
array1 = [NSMutableArray arrayWithContentsOfFile:Path1];
NSString *item = [#"dfdfDF"];
[array1 replaceObjectAtIndex:1 withObject:item];
[array1 writeToFile:Path1 atomically:YES];
NSLog(#"count: %#", [array1 objectAtIndex:1]);
you may cast or convert paths to NSString and then do:
paths = (NSString *) [paths stringByReplacingOccurrencesOfString:#"|" withString:#";"];
if this does't work, create new NSString instance that containing pathArr text, invoke replaceOccurrences method and do invert conversion
NSMutableString *tempStr = [[NSMutableString alloc] init];
for (int i = 0; i < [paths count]; i++)
{
[tempStr appendString:[path objectAtIndex:i]];
}
then use this method for tempStr. And then try:
NSArray *newPaths = [tempStr componentsSeparatedByString:#";"];
may be last method not completely correct, so try experiment with it.
Uh, why don't you just go:
NSString *cleanedString = [[[dic valueForKey:#"PATH"] objectAtIndex:0] stringByReplacingOccurrencesOfString:#";" withString:#"|"];
If there are more than one nested array, you can go
for(int i = 0; i < [[dic valueForKey:#"PATH"] count]; i++)
{
NSString *cleanedString = [[[dic valueForKey:#"PATH"] objectAtIndex:i] stringByReplacingOccurrencesOfString:#";" withString:#"|"];
// do something with cleanedString
}

NSMutableArray EXC_BAD_ACCESS (code=1)

I have no idea why, but my NSMutableArray 'items' will not take more than 5 elements.
Can someone please help? I'm following the Big Nerd Ranch iOS Programming book.
This code works fine:
NSMutableArray *items = [[NSMutableArray alloc] init];
for (int i = 5; i < 10; i++) {
BNRItem *p = [BNRItem randomItem];
[items addObject:p];
}
However if I change the initial value of i to 4 or less the program crashes when exiting the for loop:
NSMutableArray *items = [[NSMutableArray alloc] init];
for (int i = 4; i < 10; i++) {
BNRItem *p = [BNRItem randomItem];
[items addObject:p];
}
Error screenshot: http://db.tt/3CdueSYh
Change your
NSArray *randomNounList = [NSArray arrayWithObjects:#"Bear", #"Spork", "Mac", nil];
to:
NSArray *randomNounList = [NSArray arrayWithObjects:#"Bear", #"Spork", #"Mac", nil];
You forgot # before "Mac"
Hope it helps
In the screenshot you posted in your comments you are adding a C string, "Mac", to your randomNounList array. You need to make this an NSString with an # symbol.
I suspect the crash is occurring when this entry is randomly selected.
I'm surprised this compiled, I suspect you are ignoring some warnings.

Comparing strings in objective c

NSString *title = [myWebView
stringByEvaluatingJavaScriptFromString:#"document.title"];
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:8];
int j = 0;
int i=0;
int count=0;
int len;
len = [title length];
for(i=0;i<len;i++){
NSString *c = [title substringWithRange:NSMakeRange(i, 1)];
if([c isEqualToString:#","])
{
//array[count]= [title substringWithRange:NSMakeRange(j, 2)];
NSString *xxx = [title substringWithRange:NSMakeRange(j,(i-j))];
NSLog(xxx);
//insert the string into array
[array insertObject:xxx atIndex:count];
j=i;
count = count + 1;
}
}
My app always crashes at the line
[c isEqualToString:#","]
and gives the error - Thread1 : Program received signal: "SIGBART".
I know for sure that the problem is occurring while comparing strings since the app runs if I remove that one line of code.
Can someone please help? Thanks
Consider using:
- (NSArray *)componentsSeparatedByString:(NSString *)separator
Example:
NSString *title = [myWebView stringByEvaluatingJavaScriptFromString:#"document.title"];
NSMutableArray *array = [title componentsSeparatedByString:#","];
If I ends up longer than the string, or is undefined, you've got issues. If you replace i with 1, it doesn't crash, right?
Incidentally, you could use:
unichar c = [title characterAtIndex:i];