LotteryAppDelegate *appDelegate = (LotteryAppDelegate *)[[UIApplication sharedApplication] delegate];
Lotterycheck *check;
//NSString *trueval;
BOOL found = NO;
NSUInteger f;
for (f = 0; f < [appDelegate.books count]; f++) {
check = [appDelegate.books objectAtIndex:f];
checkthis = [NSString stringWithFormat:#"%#", check.LotteryNumber];
mystring =#"1234567"; //check.LotteryNumber;
NSString *finel = checkthis;
NSLog(#"Dynamic Value: %#",finel);
NSLog(#"Static Value: %#",mystring);
if ([mystring isEqualToString:finel]) {
found = YES;
[self showalert:finel];
break;
}
its not comapring the string " if([mystring isEqualToString:finel])" mystring is static value and finel is the value i am getting from the class lotterycheck..
Assuming that an object belonging to appDelegate.books is of class Book, try the following code snippet.
BOOL found = NO;
NSString *mystring = #"1234567";
for (Book *book in appDelegate.books) {
NSString *checkthis = [NSString stringWithFormat:#"%#", book.LotteryNumber];
if ([mystring isEqualToString checkthis]) {
found = YES;
[self showalert:checkthis];
break;
}
}
Related
I have a two string like "SANFRANSICO" and "CHICAGO"
Now i want to make it like the string is in reverse but in a single string like "OOCGIASCNIAHRCFNAS"
I have done code but it gives me wrong result
check out my code
- (NSString *)changeString1:(NSString *)string string2:(NSString *)string2{
int first = [string length];
int second = [string2 length];
int total = first+second;
NSMutableString *str = [[NSMutableString alloc] init];
string = [self reverseString:string];
string2 = [self reverseString:string2];
for (int i=0; i<total+1; i++)
{
if (i%2==1) {
int j = i/2;
if(j < second){
NSString *ichar = [NSString stringWithFormat:#"%c", [string2 characterAtIndex:j]];
[str appendString:ichar];
}
else{
NSString *ichar = [NSString stringWithFormat:#"%c", [string characterAtIndex:j+1]];
NSLog(#"222 %d %#",j+1, ichar );
[str appendString:ichar];
check = YES;
}
}
else {
int j = i/2;
if(check == YES){
}
else{
NSString *ichar = [NSString stringWithFormat:#"%c", [string characterAtIndex:j]];
NSLog(#"1111 %d %#",j, ichar );
[str appendString:ichar];
}
}
}
return str;
}
I found the answer very soon after post this question sorry for posting this question
check out my answer
- (NSString *)changeString1:(NSString *)string string2:(NSMutableString *)string2{
int first = [string length];
int second = [string2 length];
string = [self reverseString:string];
string2 = [self reverseString:string2];
for(int i =second; i<first; i ++){
[string2 appendString:#" "];
}
int total = first*2;
NSMutableString *str = [[NSMutableString alloc] init];
for (int i=0; i<total; i++)
{
if (i%2==1) {
int j = i/2;
if(j < second){
NSString *ichar = [NSString stringWithFormat:#"%c", [string2 characterAtIndex:j]];
[str appendString:ichar];
}
}
else {
int j = i/2;
if(j < first){
NSString *ichar = [NSString stringWithFormat:#"%c", [string characterAtIndex:j]];
[str appendString:ichar];
}
else{
NSString *ichar = [NSString stringWithFormat:#"%c", [string2 characterAtIndex:j]];
[str appendString:ichar];
}
}
}
NSLog(#"str %#", str);
return str;
}
I am working for Qr generator application and for that I have nice kyupay Library.
In that Library I need to pass string value and it returns QR Image.
It's working nice for text,Email and etc...
But Now i wants to create QR Image for Contact and Event And i don't know how to do this.
so My problem is what should be the logic for that ????
please suggest me or if you have code for that then please Share it.
Thanks for respond me on my question.
I finally creates a function that returns contact Qr code string.
It's easy but too long.
we have to check for data inputs from user and append sting as per data and create string.
I am Posting my code so it may help others and save theirs time.
-(NSString *)GenerateStringForContact{
NSString *str = #"";
NSMutableArray *arr1 =[[NSMutableArray alloc] init];
for (int i=0 ; i<[arrayAttributeName count]; i++) {
if ([[arrayAttributeName objectAtIndex:i] isEqualToString:#"First Name"]) {
if ([strContactType isEqualToString:#"ContactMecard"]) {
[arr1 addObject:#"N:"];
}else{
[arr1 addObject:#"FN:"];
}
}else if ([[arrayAttributeName objectAtIndex:i] isEqualToString:#"Last Name"]) {
// [arr1 addObject:#"N:"];
}else if ([[arrayAttributeName objectAtIndex:i] isEqualToString:#"Phone"]) {
[arr1 addObject:#"TEL:"];
}else if ([[arrayAttributeName objectAtIndex:i] isEqualToString:#"Email"]) {
[arr1 addObject:#"EMAIL:"];
}else if ([[arrayAttributeName objectAtIndex:i] isEqualToString:#"URL"]) {
[arr1 addObject:#"URL:"];
}else if ([[arrayAttributeName objectAtIndex:i] isEqualToString:#"Nickname"]) {
[arr1 addObject:#"NICKNAME:"];
}else if ([[arrayAttributeName objectAtIndex:i] isEqualToString:#"Title"]) {
[arr1 addObject:#"TITLE:"];
}else if ([[arrayAttributeName objectAtIndex:i] isEqualToString:#"Organization"]) {
[arr1 addObject:#"ORG:"];
}else if ([[arrayAttributeName objectAtIndex:i] isEqualToString:#"Birthday"]) {
[arr1 addObject:#"BDAY:"];
}else if ([[arrayAttributeName objectAtIndex:i] isEqualToString:#"Note"]) {
[arr1 addObject:#"NOTE:"];
}
}
NSMutableArray *contDetail = arrayAttributeValue;
if ([strContactType isEqualToString:#"ContactMecard"]) {
str = #"MECARD:";
// arr1 = [[NSArray alloc] initWithObjects:#"N:",#"TEL:",#"EMAIL:",#"URL:",#"NOTE:",#"NICKNAME:",#"BDAY:",#"ADR:",nil];
}else{
str = #"BEGIN:VCARD";
// arr1 = [[NSArray alloc] initWithObjects:#"N:",#"TEL:",#"EMAIL:",#"URL:",#"NOTE:",#"NICKNAME:",#"BDAY:",#"ADR:",#"TITLE:",#"ORG:",nil];
}
BOOL count = TRUE;
for (int i =0; i <[contDetail count]; i++) {
if (![[contDetail objectAtIndex:i] isEqualToString:#""]) {
NSString *tmp2 =#"";
if (i == 0 || i == 1) {
if (count) {
if ([strContactType isEqualToString:#"ContactMecard"]) {
tmp2 = [NSString stringWithFormat:#"%#,%#",[contDetail objectAtIndex:0],[contDetail objectAtIndex:1]];
NSString *tmp = [NSString stringWithFormat:#"%#%#",[arr1 objectAtIndex:i],tmp2];
str = [NSString stringWithFormat:#"%#;%#",str,tmp];
}else{
tmp2 = [NSString stringWithFormat:#"%# %#",[contDetail objectAtIndex:0],[contDetail objectAtIndex:1]];
NSString *tmp = [NSString stringWithFormat:#"%#%#",[arr1 objectAtIndex:i],tmp2];
str = [NSString stringWithFormat:#"%#\n%#",str,tmp];
}
count = FALSE;
}
}else{
tmp2 = [contDetail objectAtIndex:i];
NSString *tmp = [NSString stringWithFormat:#"%#%#",[arr1 objectAtIndex:i-1],tmp2];
if ([strContactType isEqualToString:#"ContactMecard"]) {
str = [NSString stringWithFormat:#"%#;%#",str,tmp];
}else{
str = [NSString stringWithFormat:#"%#\n%#",str,tmp];
}
}
}
}
if (![strContactType isEqualToString:#"ContactMecard"]) {
str = [NSString stringWithFormat:#"%#\nEND:VCARD",str];
}
NSLog(#"here string for generate code %#",str);
return str;
}
Thanks....
If you create a Contact QR code at this site: http://zxing.appspot.com/generator/ it provides for you the string it uses to create the QR Code. Maybe you can use this as inspiration?
How can I parse a mailto request ?
'mailto:someone#example.com?cc=someone_else#example.com&subject=This%20is%20the%20subject&body=This%20is%20the%20body'
From this NSURL, I want to extract the recipient, the subject and the body. How should I do ?
Thanks
Here is some code that will parse any URL and return a dictionary with the parameters and the associated objects in a dictionary. It works for mailto URLs, too.
Please note: This code assumes you're using ARC!
#interface NSString (URLDecoding)
- (NSString *) URLDecodedString;
#end
#implementation NSString (URLDecoding)
- (NSString *) URLDecodedString {
NSString *result = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (__bridge CFStringRef)self, CFSTR(""), kCFStringEncodingUTF8);
return result;
}
#end
- (NSDictionary *) parameterDictionaryFromURL:(NSURL *)url {
NSMutableDictionary *parameterDictionary = [[NSMutableDictionary alloc] init];
if ([[url scheme] isEqualToString:#"mailto"]) {
NSString *mailtoParameterString = [[url absoluteString] substringFromIndex:[#"mailto:" length]];
NSUInteger questionMarkLocation = [mailtoParameterString rangeOfString:#"?"].location;
[parameterDictionary setObject:[mailtoParameterString substringToIndex:questionMarkLocation] forKey:#"recipient"];
if (questionMarkLocation != NSNotFound) {
NSString *parameterString = [mailtoParameterString substringFromIndex:questionMarkLocation + 1];
NSArray *keyValuePairs = [parameterString componentsSeparatedByString:#"&"];
for (NSString *queryString in keyValuePairs) {
NSArray *keyValuePair = [queryString componentsSeparatedByString:#"="];
if (keyValuePair.count == 2)
[parameterDictionary setObject:[[keyValuePair objectAtIndex:1] URLDecodedString] forKey:[[keyValuePair objectAtIndex:0] URLDecodedString]];
}
}
}
else {
NSString *parameterString = [url parameterString];
NSArray *keyValuePairs = [parameterString componentsSeparatedByString:#"&"];
for (NSString *queryString in keyValuePairs) {
NSArray *keyValuePair = [queryString componentsSeparatedByString:#"="];
if (keyValuePair.count == 2)
[parameterDictionary setObject:[[keyValuePair objectAtIndex:1] URLDecodedString] forKey:[[keyValuePair objectAtIndex:0] URLDecodedString]];
}
}
return [parameterDictionary copy];
}
And here is how you use it:
NSURL *mailtoURL = [NSURL URLWithString:#"mailto:foo#example.com?cc=bar#example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"];
NSDictionary *parameterDictionary = [self parameterDictionaryFromURL:mailtoURL];
NSString *recipient = [parameterDictionary objectForKey:#"recipient"];
NSString *subject = [parameterDictionary objectForKey:#"subject"];
NSString *body = [parameterDictionary objectForKey:#"body"];
EDIT:
I updated the code to work with any URL and recipients are now in the dictionary for mailto URLs.
I would pull the email from that like this:
NSString * mailToString = #"'mailto:someone#example.com?cc=someone_else#example.com&subject=This%20is%20the%20subject&body=This%20is%20the%20body'";
NSArray *tempArray = [mailToString componentsSeparatedByString:#"?"];
//get email address from array
NSString * emailString = [[tempArray objectAtIndex:0]description];
//clean up string
emailString = [emailString stringByReplacingOccurrencesOfString:#"'mailto:" withString:#""];
//and here is your email string
NSLog(#"%#",emailString);
Since iOS 7 this is easily doable with NSURLComponents. You can create that object with:
if let components = NSURLComponents(URL: url, resolvingAgainstBaseURL:false) { ...
Then you can get the recipient accessing the path property of NSURLComponents; and the parameters with the queryItems property. For instance, if we wanted to get the subject, something like this would do our job
let queryItems = components.queryItems as? [NSURLQueryItem]
let subject = queryItems?.filter({$0.name == "subject"}).first?.value
NSURL category for just mailto: This method also has a fix for a crash bug in Fabian's answer above when mailto: url doesn't contain a ?. It also doesn't require the URLDecodedString category method.
#implementation NSURL (Additions)
- (NSDictionary *) parameterDictionaryForMailTo {
NSMutableDictionary *parameterDictionary = [[NSMutableDictionary alloc] init];
NSString *mailtoParameterString = [[self absoluteString] substringFromIndex:[#"mailto:" length]];
NSUInteger questionMarkLocation = [mailtoParameterString rangeOfString:#"?"].location;
if (questionMarkLocation != NSNotFound) {
[parameterDictionary setObject:[mailtoParameterString substringToIndex:questionMarkLocation] forKey:#"recipient"];
NSString *parameterString = [mailtoParameterString substringFromIndex:questionMarkLocation + 1];
NSArray *keyValuePairs = [parameterString componentsSeparatedByString:#"&"];
for (NSString *queryString in keyValuePairs) {
NSArray *keyValuePair = [queryString componentsSeparatedByString:#"="];
if (keyValuePair.count == 2)
[parameterDictionary setObject:[[keyValuePair objectAtIndex:1] stringByRemovingPercentEncoding] forKey:[[keyValuePair objectAtIndex:0] stringByRemovingPercentEncoding]];
}
}
else {
[parameterDictionary setObject:mailtoParameterString forKey:#"recipient"];
}
return [parameterDictionary copy];
}
- (NSDictionary *) parameterDictionaryFromURL:(NSURL *)url {
NSMutableDictionary *parameterDictionary = [[NSMutableDictionary alloc] init];
NSURLComponents * urlComponents = [NSURLComponents componentsWithString:url.absoluteString];
for (NSURLQueryItem *item in urlComponents.queryItems) {
parameterDictionary[item.name] = item.value;
}
if ([url.scheme isEqualToString:#"mailto"]) {
NSUInteger questionMarkLocation = [url.resourceSpecifier rangeOfString:#"?"].location;
if (questionMarkLocation == NSNotFound) {
parameterDictionary[#"recipient"] = url.resourceSpecifier;
} else {
parameterDictionary[#"recipient"] = [url.resourceSpecifier substringToIndex:questionMarkLocation];
}
}
return [parameterDictionary copy];
}
I am getting a memory leak in a method that builds a email message string from a NSManagedObject. The string is created with NSString convenience methods, and so should be autoreleased. What am I missing? Code below... Instruments is flagging the final occurance of buildString near the bottom of the method (see comment in code):
-(void)buildEmailMessage {
//check for presence of lat and lon data
BOOL hasStartLatLon = NO;
BOOL hasEndLanLon = NO;
NSString *startLat;
NSString *startLong;
NSString *endLat;
NSString *endLong;
NSString *mapURL;
NSString *finalMapURL;
if( [managedObject valueForKey:#"startLat"] > 0 ){
hasStartLatLon = YES;
startLat = [self formatLatLon:[managedObject valueForKey:#"startLat"]];
startLong= [self formatLatLon:[managedObject valueForKey:#"startLong"]];
}
if( [managedObject valueForKey:#"endLat"] > 0 ) {
hasEndLanLon = YES;
endLat = [self formatLatLon:[managedObject valueForKey:#"endLat"]];
endLong= [self formatLatLon:[managedObject valueForKey:#"endLong"]];
}
// Build strings from managedObject
// Start with the trip info already validated
NSString *tripName = [managedObject valueForKey:#"tripName"];
NSString *intro = [NSString stringWithString:#"Please contact the approriate authorities and provide them with the enclosed information if our party does not return withing 12 hours of the return date shown below. Thanks."];
id vStartDate = [managedObject valueForKey:#"startDate"];
NSString *startDate = [NSString stringWithFormat:#"Start Date: %#", [vStartDate managedObjectValueDisplay]];
id vEndDate = [managedObject valueForKey:#"endDate"];
NSString *endDate = [NSString stringWithFormat:#"End Date: %#", [vEndDate managedObjectValueDisplay]];
NSString *startFrom = [NSString stringWithFormat:#"Departing from:%#", [managedObject valueForKey:#"startFrom"]];
if( hasStartLatLon ){
startFrom = [startFrom stringByAppendingString:#""];
startFrom = [startFrom stringByAppendingString:[self getLatLon:#"start"]];
}
startFrom = [startFrom stringByAppendingString:#""];
NSString *endAt = [NSString stringWithFormat:#"Returning to:%#", [managedObject valueForKey:#"endAt"]];
if( hasStartLatLon ){
endAt = [endAt stringByAppendingString:#""];
endAt = [endAt stringByAppendingString:[self getLatLon:#"end"]];
}
endAt = [startFrom stringByAppendingString:#""];
// Add a link to Google Maps if there is geodata
if (hasStartLatLon || hasEndLanLon) {
if (hasStartLatLon) {
mapURL = [NSString stringWithFormat:#"http://maps.google.com/?q=%#,%#+(%#)&ll=%#,%#&z=14&t=p", startLat, startLong, [managedObject valueForKey:#"startFrom"], startLat, startLong];
} else {
mapURL = [NSString stringWithFormat:#"http://maps.google.com/?q=%#,%#+(%#)&ll=%#,%#&z=14&t=p", endLat, endLong, [managedObject valueForKey:#"endAt"], endLat, endLong];
}
NSString* escapedUrlString =[mapURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"escaped map url = %#", escapedUrlString);
finalMapURL = [NSString stringWithFormat:#"Link to Google Map:%#", escapedUrlString];
}
NSString *routeInfo = [NSString stringWithFormat:#"Route Information:%# ", [managedObject valueForKey:#"routeInfo"]];
// Check for vehicle info
BOOL hasVehicleMakeAndModel = NO;
BOOL hasVehicleLicenseNumber = NO;
BOOL hasVehicleState = NO;
NSString *vehicleMakeAndModel = [managedObject valueForKey:#"vehicleMakeAndModel"];
NSString *vehicleLicenseNumber = [managedObject valueForKey:#"vehicleLicenseNumber"];
NSString *vehicleState = [managedObject valueForKey:#"vehicleState"];
if ( vehicleMakeAndModel.length > 0 ) {
hasVehicleMakeAndModel = YES;
}
if ( vehicleLicenseNumber.length > 0 ) {
hasVehicleLicenseNumber = YES;
}
if ( vehicleState.length > 0 ) {
hasVehicleState = YES;
}
//Build the vehicle string
NSString *vehicleString;
if (hasVehicleMakeAndModel || hasVehicleLicenseNumber || hasVehicleState) {
vehicleString = [NSString stringWithString:#"Vehicle Information:"];
}
if (hasVehicleMakeAndModel) {
vehicleString = [vehicleString stringByAppendingString:#"Make/Model: "];
vehicleString = [vehicleString stringByAppendingString:vehicleMakeAndModel];
vehicleString = [vehicleString stringByAppendingString:#""];
}
if (hasVehicleLicenseNumber) {
vehicleString = [vehicleString stringByAppendingString:#"License #: "];
vehicleString = [vehicleString stringByAppendingString:vehicleLicenseNumber];
vehicleString = [vehicleString stringByAppendingString:#""];
}
if (hasVehicleState) {
vehicleString = [vehicleString stringByAppendingString:#"State: "];
vehicleString = [vehicleString stringByAppendingString:vehicleState];
}
// Get the NSSet of party members from the managedObject
// and build the party members/emergency contact info
NSSet *membersSet = [managedObject valueForKey:#"members"];
NSString *membersString;
if ( [membersSet count] > 0) {
membersString = #"Party Members:";
NSArray *membersArray = [NSArray arrayByOrderingSet:membersSet byKey:#"lastName" ascending:YES];
for (NSManagedObject *oneObject in membersArray) {
BOOL hasFirstName = NO;
BOOL hasLastName = NO;
BOOL hasAge = NO;
BOOL hasContactName = NO;
BOOL hasContactNumber = NO;
NSString *memberFirstName = [oneObject valueForKey:#"firstName"];
NSString *memberLastName = [oneObject valueForKey:#"lastName"];
NSNumber *memberAgeNum = [oneObject valueForKey:#"age"];
NSString *memberAgeString;
if (memberAgeNum > 0) {
memberAgeString = [NSString stringWithFormat:#"%d", [memberAgeNum intValue]];
} else {
memberAgeString = [NSString stringWithString:#""];
}
NSString *contactName = [oneObject valueForKey:#"contactName"];
NSString *contactNumber = [oneObject valueForKey:#"contactNumber"];
if ( [memberFirstName length] > 0) {
hasFirstName = YES;
}
if ( [memberLastName length] > 0) {
hasLastName = YES;
}
if ( [memberAgeString length] > 0) {
hasAge = YES;
}
if ( [contactName length] > 0) {
hasContactName = YES;
}
if ( [contactNumber length] > 0) {
hasContactNumber = YES;
}
NSString *oneMemberString = [NSString stringWithString:#""];
if (hasFirstName) {
oneMemberString = [oneMemberString stringByAppendingFormat:#"%# ", memberFirstName];
}
if (hasLastName) {
oneMemberString = [oneMemberString stringByAppendingString:memberLastName];
}
if (hasAge) {
oneMemberString = [oneMemberString stringByAppendingFormat:#", %#", memberAgeString];
}
if (hasContactName) {
oneMemberString = [oneMemberString stringByAppendingFormat:#"Emergency Contact:%#", contactName];
}
if (hasContactNumber) {
oneMemberString = [oneMemberString stringByAppendingFormat:#"Phone: %#", contactNumber];
}
membersString = [membersString stringByAppendingString:oneMemberString];
}
}
NSString *buildString = [NSString stringWithFormat:#"Trip Plan for:%#", tripName];
buildString = [buildString stringByAppendingString: intro];
buildString = [buildString stringByAppendingString: startDate];
buildString = [buildString stringByAppendingString: endDate];
buildString = [buildString stringByAppendingString: startFrom];
buildString = [buildString stringByAppendingString: endAt];
if (hasStartLatLon || hasEndLanLon) buildString = [buildString stringByAppendingString: finalMapURL];
buildString = [buildString stringByAppendingString: routeInfo];
// Append the vehicle string if any vehicle data is present
if (hasVehicleMakeAndModel || hasVehicleLicenseNumber || hasVehicleState) {
buildString = [buildString stringByAppendingString: vehicleString];
}
// Append the members string if any members data is present
// **MEMORY LEAK** flagged on the line enclosed by the if statement below
if ( [membersSet count] > 0) {
buildString = [buildString stringByAppendingString: membersString];
}
self.myEmailString = [NSString stringWithString:buildString];
}
-(NSString *)formatLatLon:(NSNumber *)value {
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setPositiveFormat:#"##0.00000"];
NSString *returnString = [formatter stringFromNumber:value];
[formatter release];
return returnString;
}
How is your myEmailString #property defined? With copy or retain? (You should use copy with NSString objects.) If so, and you're not doing a [myEmailString release] in dealloc, that might be the leak.
I've put UIsegment Control in my IPhone application.To load the pages I've this function
- (void) segmentSelected:(id)segmentedCntl{
NSLog(#"Selected Segment Index = %d", [segmentedCntl selectedSegmentIndex]);
int index = [segmentedCntl selectedSegmentIndex];
ViewController *viewController = (ViewController *) controller;
NSString *xPath = [NSString stringWithFormat:#"item[%d]/a", (index + 1)];
NSArray *items = [element nodesForXPath:xPath error:nil];
if([items count] > 0){
CXMLElement *itemElement = (CXMLElement *) [items objectAtIndex:0];
NSString *url = [[itemElement attributeForName:#"href"] stringValue];
NSLog(#"url is &&&&&&&&&&&&&&&& %#", url);
if([url startsWith:#"#"]){
}else{
NSString *fullURL = [URLLoader getURL:url];
if(fullURL != nil){
if([fullURL contains:#"?"]){
fullURL = [fullURL stringByAppendingString:#"&iPhoneMode=app"];
}else{
fullURL = [fullURL stringByAppendingString:#"?iPhoneMode=app"];
}
[viewController showURL:fullURL cacheDuration:kPageCacheDuartion];
}
}
}
}
But the segmentControl is not clickable.Please help me to solve this problem
Namastey saikamesh
Please check proper u'r UISegmentControl property setting it will be working.