Category Archives: Swift

Error reporting for complex unit tests

System level unit testing

I was recently putting together the structure for some system level unit tests of a Swift project.   The structure extended beyond a single file for these tests.   I currently have:

  • SystemTests – Base class for a suite of system level unit tests.
  • BasicSystemTests – Class for my first group of unit tests.  Derives from SystemTests.
  • Various utility classes, each specific to a major section of the project.

Here is an example chain of calls coming from a test case:

BasicSystemTests

   func testModified () {
        
        // Test modification to Project
        //
        createProject(name: "Test Project", bundleId: "TestBundleId", appDelegate: "TestAppDelegate", fsConfig: "TestFsConfig", description: "Test Project Description")
        

SystemTests


   func createProject(name: String, bundleId: String, appDelegate: String?, fsConfig: String?, description: String?) -> String? {
        
        // Is Add New button enabled?
        project.verifyAddNewButton(enabled: false)

ProjectTestUtils

    func verifyAddNewButton(enabled: Bool) {
        XCTAssertEqual(addNewButton.isEnabled, enabled, "Add New button enabled = \(enabled)")
    }
  

The problem – what is the path?

My test case has a bug.  When I run the test case, I see the following in Issue Navigator and the source code editor:

Continue reading

Advertisements

Swift framework development for binary distribution with CocoaPods

Swift Framework with Objective-C

Having previously developed some Objective-C based frameworks, I set out to explore doing the same with Swift and be able to deliver the framework as a Pod.

Creating a a Swift Framework is pretty straightforward and there are some great references on the subject.  One that I found very helpful was Creating and Distributing iOS Frameworks

I wanted to include some Objective-C based CocoaPods in my Swift Framework and quickly ran into an issue – you can’t use bridging headers with framework targets.

I did find a workaround which was interesting.  An answer in the thread Importing CommonCrypto in a Swift framework showed how to create a module that could be imported into Swift using an Aggregate target.  Using this answer as a guide, I was able to create a module for AFNetworking using the run script:

mkdir -p "${BUILT_PRODUCTS_DIR}/AFNetworkingModuleMap"
cat <<EOF > "${BUILT_PRODUCTS_DIR}/AFNetworkingModuleMap/module.modulemap"
module AFNetworking [system] {
header "${PODS_ROOT}/Headers/Public/AFNetworking/AFNetworking.h"
export *
}
EOF

This was interesting, but not the right approach.

Continue reading

Exporting a video in iOS to reduce size and ensure maximum client compatibility

Movies that lay on their side

Have you ever taken a Portrait oriented movie with your iPhone or iPad and sent it to your friends who were using Windows?   I can understand their frustration when the video I so carefully recorded causes them to bend their neck to the side:

rotated_portrait

So what’s going on here?  The problem is that the .MOV file that I send them has a 90 degree rotation set as the preferred transform.   Some Windows clients (such as Windows Media Player, VLC media player, etc) do not take this transform into consideration when presenting the video.

The VideoExport Swift Project

I wanted to create a project that would allow me to address the issue with rotations and other compatibility concerns.

I published my results to scottcarter/VideoExport on Github.

Continue reading

Logging and bookmarks with Objective-C and Swift

Overview

This article discusses the techniques I use for bookmarking code and adding log statements for both Objective-C and Swift.

ObjC_Swift_logging

Temporary Bookmarks

When I develop code I often find a need to temporarily reference different sections that I’m working on, either within the same file or in many different files.   Prior to Xcode 4 there was apparently a means to set and jump to bookmarks with keyboard shortcuts as mentioned in the article Killer Xcode Tips and Tricks – Tips 1 to 10 (see tips 7 and 8).

Continue reading

Xcode simulator bug with Swift to Objective-C call passing CMTime structure

CMTime Bug

I was in the process of creating an illustrative Swift project that performed some video conversions when I ran across an interesting bug.

The bug first manifested itself when I was attempting to set the opacity of a AVMutableVideoCompositionLayerInstruction object as part of a method that was creating a AVVideoComposition object.

videolayerInstruction.setOpacity(0.0, atTime: mytime)

Here mytime is a CMTime structure that is the AVAsset duration.

When I ran this on the simulator, I got an exception with the message “The time of an opacity setting must be numeric.”

Debugging the error

After searching Google and the Apple Developer forums, I could not find any reference to my error message or any issue related to CMTime in this context.

Since I had used similar code in Objective-C, I started the process to narrow down the error.  I first attempted to call a simple Objective-C method from Swift that would set the opacity (passing in videolayerInstruction as first arg and mytime as second arg).    This provoked the same error.  I discovered that the CMTime structure was corrupted in the Objective-C method.

Through trial and error I found that the corruption would not occur if I reversed the order of the arguments to the Objective-C call.    Corruption also did not happen if I passed the structure by reference.    A different structure type (I used CGSize) also did not encounter any corruption.

Specific situation

The end result of my investigation was that the error that I was seeing only occurred under very specific circumstances.  In particular:

  • Only with Swift to Objective C call.
  • Only on simulator with iPhone 4S and iPhone 5 configurations.
  • CMtime structure passed as second argument.

I suspect that the underlying issue may be a 32 bit versus 64 bit consideration since the iPhone 5S was the first device to introduce 64 bit processing.

Bug report and Github example project

An Apple bug report was filed today.   An example project has been uploaded to Github at https://github.com/scottcarter/CMTimeBug