How to set the system timezone for iOS by code?

Still working on Xcode 11. The swift 5 implementation would look like:

override func setUpWithError() throws {
    // Put setup code here. This method is called before the invocation of each test method in the class.
    
    //All test by default will use GMT time zone. If different, change it within the func
    TimeZone.ReferenceType.default = gmtTimeZone
}

override func tearDownWithError() throws {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    
    //Rest to current
    TimeZone.ReferenceType.default = TimeZone.current
}

In my XCTestCase class I have my time zones are declared like:

private let gmtTimeZone = TimeZone(abbreviation: "GMT")!
private let gmtPlus1TimeZone = TimeZone(abbreviation: "GMT+1")!
private let gmtMinus1TimeZone = TimeZone(abbreviation: "GMT-1")!

I am defaulting all my test to GMT but I for a specific test I want to change it, then:

func test_Given_NotGMTTimeZone_ThenAssertToGMT() {
    TimeZone.ReferenceType.default = gmtPlus1TimeZone
    ...
}

Added You can also work with time zone names like "British Summer Time"

private let britishSummerTimeZone = TimeZone(abbreviation: "BST")!

Added

This did not work for me due to time zone caching. I needed to reset the cache after each time zone change to stop the tests from interfering with each other. E.g.

TimeZone.ReferenceType.default = gmtTimeZone
TimeZone.ReferenceType.resetSystemTimeZone ()

As the others have said you can't edit the system timezone from within an app, however you can set a default timezone for your entire application using +setDefaultTimeZone: on NSTimeZone.

You mentioned this was for testing, so I'm going to assume this is for unit testing some custom date formatters and such, in which case you could do something like this in your unit test:

static NSTimeZone *cachedTimeZone;

@implementation DateUtilTests

+ (void)setUp
{
    [super setUp];
    cachedTimeZone = [NSTimeZone defaultTimeZone];
    // Set to whatever timezone you want your tests to run in
    [NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
}

+ (void)tearDown
{
    [NSTimeZone setDefaultTimeZone:cachedTimeZone];
    [super tearDown];
}

// ... do some tests ...

@end

I hope that helps!