console I/O in iOS - ios

I just started doing some competitive programmning in objective C. There's a problem which I'm facing, most sites start with main.m
int main(int argc, char * argv[]) {
#autoreleasepool {
}
}
I somehow learned how to get input and use it. But today I tried a differnt work where
void main()
{
}
when I tried scanf("%s",str) though I could read the first line of input. But I don't know how to start and how to get rest of lines.
I want to lean more about console input/output in iOS. Any help will be much appreciated?

What you need is a bit of a tutorial
If you right click any data structure for instance there'l be a first option "print description" another option with your code : NSLog(name of variable); for e.g for an array : NSLog(#"%#",myArray);
Lastly,Typing po myarray in the right half of the console (under lldb) would print out myArray's contents
Hope this helps! Cheers

Related

On iOS, are 0x0000000000000026, 0x000000000000001c, 0x000000000000005a examples of tagged pointers?

On iOS 11, when I intentionally create objects which would turn out to be tagged pointers, they start with 0xB, instead of the 0x0000000000000026, 0x000000000000001c, 0x000000000000005a values that are showing up in my crash reports as invalid addresses. I think these are likely tagged pointers, but they aren't formatted like tagged pointers that I see in the debugger.
What about 0x0000000000000010, 0x0000000000000020, 0x0000000000000030 ? They all have a trailing 0, but they sure look suspiciously small to be real pointers.
The implementation details of tagged pointers changes from release to release and architecture to architecture. With that said, those really don't look like tagged pointers.
What is most likely is that some piece of code is dereferencing into a struct or object that is unexpectedly NULL or nil.
Run this code:
struct bob {
void *a;
void *b;
void *c;
char d[42];
};
int main(int argc, const char * argv[]) {
struct bob *fred = NULL;
fred->d[2] = 'q';
return 0;
}
You'll get this crash (on x86_64): Thread 1: EXC_BAD_ACCESS (code=1, address=0x1a)
That is, it is trying to dereference through 0x0. So, more likely than not, you have a struct/object reference that is NULL and your code is trying to dereference an element or instance variable that is offset by the hex #s you listed from the beginning.

Is there a way to see every time NSLog is called and the content of the NSLog?

Ideally I'd like to setup a selector to see every time NSLog is called in my app, and to get the output of that NSLog. NSLog might be called by an SDK I'm using and I'd like to be able to see the output of those logs from within my app. Is that possible?
What you want is a Symbolic breakpoint.
Navigate to the Breakpoints tab in Xcode and use the + button in the bottom left corner to add one.
When the breakpoint is added, it should automatically come up with a window that allows you to enter the conditions on which you want it to pause execution. You can use NSLog as the symbol.
As for calling a custom function of your own, you can attempt to do that by adding an action in that breakpoint and utilise the debugger's ability to call into your own code by passing in the arguments that are available to the debugger e.g. $arg1.
So I dont think it is possible to simply replace every instance of NSLog with something else... but you could do something like this:
#define NSLog MyLog
void MyLog(NSString *format, ...) NS_FORMAT_FUNCTION(1,2);
void MyLog(NSString *format, ...)
{
va_list list;
va_start(list,format);
NSLogv(format,list);
va_end(list);
va_start(list,format);
vfprintf(stderr,[format UTF8String], list);
va_end(list);
}
int main(int argc, const char * argv[]) {
NSLog(#"hello %s\n","world");
return 0;
}
My example uses 2 va_lists to log with an NSLog like NSLogv and vfprintf to log to an open FILE * stream (stderr in this case)... you could also probably more simply format an NSString (as vfprintf can't print NS objects)...
I tried a little bit to try to re-assign NSLog with dlsym which would be process wide, rather than just when the macro is expanded... I couldn't get it to work but I am not a dynamic linker expert.

XCode 6 verificationController.m issues

I am using VerificationController.m provided by Raywenderlich for validating receipts for in ap purchase. It is working fine for XCode5 but in XCode6 it is giving number of errors. probably due to C++ code like:
Missing Code for Method declaration
#end must appear in objective-c
context Conflicting types for 'checkReiptSecurity'
can anyone tell me what is needed to be done ?
Edit : Here are errors screenshot
Have you fixed this? I was running in to the exact same problem so I'll leave my fix here for anyone that comes looking. It turns out in newer versions of Xcode you aren't allowed to put C/C++ code in objective-C context anymore. So I moved the declarations for unsigned int iTS_intermediate_der_len, unsigned char iTS_intermediate_der[], char* base64_encode(const void* buf, size_t size), and void * base64_decode(const char* s, size_t * data_len) to the top of the file, above the #implementation tag.
Have you downloaded sample code? I have downloaded sample code and its working fine at my side. It seems that you have missed or added an extra braket } or { in your code.
May be this happened when you was trying to comment this code [UIDevice currentDevice].uniqueIdentifier; because originally this line produce an error.

Objective-c-Keyboard input

I believe ive looked at every article related to keyboard input, but still cant get it to work. ALl i want is a output, using NSLog everytime i hit a key, in the app or not. Im currently using xcode 5. Ive tried many snippets of code such as
[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent *event)
NSLog(#"%#",event.characters);
and im not sure where to put his code. Do i put it in the main function like this
#import <Foundation/Foundation.h>
#import <appkit/NSEvent.h>
int main(int argc, char * argV[]) {
#autoreleasepool{
[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent *event)
NSLog(#"%#",event.characters);
}
}
Clearly im new to objective-C, and i dont plan on continuing with it unless i can get keyboard input to work. Ive tried tutorials, snippets from keyloggers, and the mac dev forums. Thanks.
Your code is pretty close but you're getting hung up on block syntax. Blocks are very powerful and useful, but the syntax is truly awful and I still struggle with it 2 years after starting to work with them. Blocks are much less readable than C function pointers, and that's saying a lot.
It should look like this:
int main(int argc, char * argV[])
{
#autoreleasepool
{
[NSEvent addGlobalMonitorForEventsMatchingMask: NSKeyDownMask
handler: ^(NSEvent *event)
{
NSLog(#"%#",event.characters);
}
];
}
}
I put all the opening and closing braces on separate lines for clarity. A block needs to be enclosed in braces, and you were missing braces, as well as the closing bracket on your call to addGlobalMonitorForEventsMatchingMask:handler:
BTW, it's very unusual to change the main() function on a Mac or iOS program. Usually you leave that alone, and then set up an application object, set up a delegate, and put your custom code in the app delegate.
You should start of using the normal Cocoa design patterns for applications, and not try to toss them.
If you want to work with main() in C style you should think about creating a command line tool intend of an interactive application. You're going to set yourself up for failure if you don't follow the standard patterns. There's just too many ways to get things wrong.
Your main is missing the housekeeping needed to create a working iOS or Mac application

Simple method call in Xcode 5 and passed value won't change

I'm a noob to Xcode and am reading the Big Nerd Ranch book and its asking me to do example programs in simple C to get me familiar with the language, however its asked me to create a program that calls a method from main and passes an int and does a square calculation and the printf to defog screen. Here is the program:-
#include <stdio.h>
void doTheMath(int numberToSquare)
{
int numberSquared = numberToSquare * numberToSquare;
printf("%d squared is %d\n",numberToSquare,numberSquared);
}
int main(int argc, const char * argv[])
{
doTheMath(5);
return 0;
}
As you can see I am passing the value 5 to the method and it prints 25 on screen when i run the code. IF I then change 5 to 15 to get it to write out a different value, it doesn't. It still writes out 5 squared, not 25 squared.
In debug and step through the value is wrong and isn't changed.
I've closed the project and Xcode and still it doesn't work all of the time and then sometimes it does reflect the changed value.
The project Type is an OSX application / command-line tool. The project is stored on my NAS.
Your code is correct and when I test it in Xcode and change the 5 to 15 and run it, I get the correct answer returned. Make sure you save your file after you make the change, and you could also try to clean your project (Product > Clean).
Sometimes Xcode just does strange things . . .

Resources